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

**URL:** <https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904>\
**Category:** Using Swift\
**Created:** [July 30, 2020, 8:35pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904 "2020-07-30T20:35:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [July 30, 2020, 8:35pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/1 "2020-07-30T20:35:21Z")

</div>

The doc:

```swift
init(wrappedValue:)
Creates the state with an initial wrapped value.

init(initialValue:)
Creates the state with an initial value.

```

why these two init's, they seem to be the same, but there must be some reason for this?

---

<div class="post-metadata">

**Author:** ![atfelix](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/atfelix/32/9310_2.png) [@atfelix](https://forums.swift.org/u/atfelix)\
**Post date:** [July 30, 2020, 8:42pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/2 "2020-07-30T20:42:18Z")

</div>

The documentation for `init(wrappedValue:)` indicates that you should never call it. It is used by the declaration of state property:

> Discussion
> 
> You don’t call this initializer directly. Instead, declare a property with the `@State` attribute, and provide an initial value; for example, `@State private var isPlaying: Bool = false` .

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [July 30, 2020, 10:31pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/3 "2020-07-30T22:31:52Z")

</div>

I saw the "discussion". I still like to know what's the difference between the two. Despite "don't call this initializer directly", you can call it anyway and I see code that calls this.

Anyway, `init(wrappedValue:)` "don't call directly", `init(initialValue:)` does not say this. There must be some difference? What is it?

---

<div class="post-metadata">

**Author:** ![harlanhaskins](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/harlanhaskins/32/333_2.png) [@harlanhaskins](https://forums.swift.org/u/harlanhaskins)\
**Post date:** [July 30, 2020, 10:41pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/4 "2020-07-30T22:41:53Z")

</div>

`init(initialValue:)` was the first name chosen when the original Property Wrappers proposal was released. It was changed to `init(wrappedValue:)` but the original one was left for backwards compatibility.

> <https://github.com/apple/swift-evolution/blob/master/proposals/0258-property-wrappers.md>

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [July 30, 2020, 10:44pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/5 "2020-07-30T22:44:35Z")

</div>

That's my initial thought, but doesn't the SE-0258 review ends a good while before the announcement of SwiftUI, and most definitely long before the beta test ends?

---

<div class="post-metadata">

**Author:** ![harlanhaskins](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/harlanhaskins/32/333_2.png) [@harlanhaskins](https://forums.swift.org/u/harlanhaskins)\
**Post date:** [July 30, 2020, 10:45pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/6 "2020-07-30T22:45:23Z")

</div>

Yeah, SwiftUI could have removed the original one before shipping. But now that it's shipped, it's ABI and cannot be removed.

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [July 30, 2020, 11:26pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/7 "2020-07-30T23:26:56Z")

</div>

Should `init(initialValue:)` be "deprecated"?

And the discussion "don't call directly" is not good because [you need to call it directly sometimes](https://forums.swift.org/t/assignment-to-state-var-in-init-doesnt-do-anything-but-the-compiler-gened-one-works/35235).

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [July 31, 2020, 8:56am UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/8 "2020-07-31T08:56:16Z")

</div>

> [@atfelix](#):
>
> The documentation for `init(wrappedValue:)` indicates that you should never call it.

I don't know where you got this from as this isn't correct. You can and must use it if you need to initialize the property wrapper by hand. If your PW has an `init(initialValue:)` overload, you should just ignore it and prefer `init(wrappedValue:)`, that's all.

In `Combine` framework, they had this issue for a whole year. Basically they shipped with only `init(initialValue:)` but added `init(wrappedValue:)` a year later and also back ported it using `@_alwaysEmitIntoClient`. 🤦‍♂️

---

<div class="post-metadata">

**Author:** ![atfelix](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/atfelix/32/9310_2.png) [@atfelix](https://forums.swift.org/u/atfelix)\
**Post date:** [July 31, 2020, 11:15am UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/10 "2020-07-31T11:15:26Z")

</div>

Apple Documentation: [Apple Developer Documentation](https://developer.apple.com/documentation/swiftui/state/init(wrappedvalue:))

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [July 31, 2020, 11:17am UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/11 "2020-07-31T11:17:25Z")

</div>

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:

```swift
@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.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [July 31, 2020, 4:23pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/12 "2020-07-31T16:23:14Z")

</div>

Except we have to use direct initialization if we want to provide an initial value. Initializing just the wrapped value doesn't trigger the wrapper's effects. Is there an alternate solution here?

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [July 31, 2020, 4:38pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/13 "2020-07-31T16:38:23Z")

</div>

I don‘t understand the question, do you have a small example of what you want to achieve and how it‘s not possible/working?!

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [July 31, 2020, 4:43pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/14 "2020-07-31T16:43:52Z")

</div>

Say you have a `View` with a `Picker` inside and want to set the initial selection from outside the view. Just passing the selection and setting it on the state property doesn’t work, but initializing the `State` wrapper directly does.

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [July 31, 2020, 4:57pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/15 "2020-07-31T16:57:57Z")

</div>

I don't understand what you're saying...show sample code would clear things for me.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [July 31, 2020, 5:34pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/16 "2020-07-31T17:34:30Z")

</div>

That‘s an anti pattern anyway because of the re-injection for `State`. You should create a model from a different place and pass it down. Again, that‘s not a property wrapper issue.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [July 31, 2020, 5:43pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/17 "2020-07-31T17:43:50Z")

</div>

Whatever model I pass down would have to be translated into a binding for the `Picker`, so I'm not sure what you're suggesting here.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [July 31, 2020, 6:07pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/18 "2020-07-31T18:07:22Z")

</div>

```swift
struct MyView: View {
  class Model: ObservableObject {
    @Published
    var selection: Int
    init(selection: Int) {
      self.selection = selection
    }
  }
  
  @ObservedObject
  var model: Model

  var body: some View {
    Picker(selection: $model.selection, ...)
  }
}

```

Initialize the model from the parent for example using `StateObject` to persist it, pass it down and use the property wrappers projected value to obtain a binding.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [July 31, 2020, 6:35pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/19 "2020-07-31T18:35:09Z")

</div>

So to merely set the default value of a `Picker` we need to create a separate type, own an instance outside the view that cares about it, inject it into the view, and use that binding? Even with the new `@StateObject`, that doesn't seem right.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [July 31, 2020, 6:37pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/20 "2020-07-31T18:37:14Z")

</div>

That‘s the way to do it if you really want to provide a different initial value than a constant default value for `MyView`.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [July 31, 2020, 7:04pm UTC](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904/21 "2020-07-31T19:04:50Z")

</div>

SwiftUI views don't really have concepts of lifetime. So it's hard to say when a view is _recreated_ and needs to reload the initial values.

If your _reload_ events coincide with the view appearances though, you can just use `onAppear`.

[Next page](https://forums.swift.org/t/swiftui-state-init-wrappedvalue-vs-init-initialvalue-whats-the-difference/38904.md?page=2)
