4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SwiftUIで@StateObjectをinitで初期化する

Posted at

以下のように_をつけて代入してやることで初期値を設定できました。

xxx.swift
struct XxxView: View {
    @StateObject var xxxVM: XxxViewModel
    @State var name: String = "";

    init(user: User) {
        _xxxVM = StateObject(
            wrappedValue: XxxViewModel()
        )
        _name = State(initialValue: user.name)
    }


    init(url: URL) {
        _xxxVM = StateObject(
            wrappedValue: XxxViewModel(url: url)
        )
    }
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?