SwiftUI.State: .init(wrappedValue:) vs. .init(initialValue:): what's the difference?

That is an API contract not a general PW thing. State has some special framework specific behavior which they communicate through documentation and WWDC videos. If you use the init to inject value from a parent, you will tap into bugs.

The following example is totally valid:

@propertyWrapper
struct Wrapper {
  let wrappedValue: Int // `init(wrappedValue:)` synthesized
}

struct T {
  @Wrapper
  var a: Int = 0

  @Wrapper
  var b: Int

  init(b: Int) {
    self._b = Wrapper(wrappedValue: b * 2)
  } 
}

Long story short, ignore init(initialValue:) on property wrapper types that has been already shipped. It's likely you never will see such an init on future property wrappers unless it provides the user some totally different semantics.

1 Like