Cannot use instance member 'restaurantOptions' within property initializer; property initializers run before 'self' is available

I am trying to create a large string from the strings in the array that I stored in UserDefaults but it gives me this error and I don't understand what it means:

Cannot use instance member 'restaurantOptions' within property initializer; property initializers run before 'self' is available

Code Block Swift
import SwiftUI
struct RestaurantPickerView: View {
   
  @State var optionString:String = ""
  @State var randChoice:String = ""
  @State var restaurantOptions:Array = UserDefaults.standard.stringArray(forKey: "restaurantOptions") ?? []
  @State var restaurantString:String = restaurantOptions.joined(separator: "\n") // Error Occurs Here
  @State var currentOption:String = ""
  // Ignore Below
  var body: some View {
    ZStack {
      Color(red: 1.003, green: 0.597, blue: 0.449)
        .ignoresSafeArea()
      if randChoice != "" {
        VStack (alignment: .center){
          Text("Choice:")
          Text(randChoice)
            .padding()
          Button(action: {
            randChoice = ""
          }) {
            Text("Clear")
              .foregroundColor(Color(red: 0.986, green: 0.379, blue: 0.125))
              .padding()
              .background(Color(red: 0.649, green: 0.856, blue: 0.908))
              .cornerRadius(20)
          }
        }
      } else {
        VStack {
          Text("Input Choices")
          TextField("Input Here", text: $currentOption)
            .padding()
            .multilineTextAlignment(.center)
          Button(action: {
            restaurantOptions.append(currentOption)
            optionString = optionString + currentOption + "\n"
            currentOption = ""
             
            UserDefaults.standard.set(restaurantOptions, forKey: "restaurantOptions")
             
          }) {
            Text("Enter")
              .foregroundColor(Color(red: 0.986, green: 0.379, blue: 0.125))
              .padding()
              .background(Color(red: 0.649, green: 0.856, blue: 0.908))
              .cornerRadius(20)
          }
          if optionString != "" {
            Text(optionString)
              .multilineTextAlignment(.center)
              .padding()
          }
          if restaurantOptions.count > 1 {
            Button(action: {
              randChoice = restaurantOptions.randomElement()!
            }) {
              Text("Pick")
                .foregroundColor(Color(red: 0.986, green: 0.379, blue: 0.125))
                .padding()
                .background(Color(red: 0.649, green: 0.856, blue: 0.908))
                .cornerRadius(20)
            }
          }
        }
      }
    }
  }
}

Cannot use instance member 'restaurantOptions' within property initializer; property initializers run before 'self' is available

In your code, restaurantOptions is an instance property, OK?
And restaurantString also is an instance property.
And you are trying to use restaurantOptions.joined(separator: "\n") as an initial value of the property restaurantString,
in other words, you are trying to use restaurantOptions.joined(separator: "\n") within property initializer.

In Swift, you cannot use any instance methods nor any instance properties in an initial value of other (usual) instance properties.
The error message is re-stating the fact with very simplified reason.


One way to workaround this restriction of Swift, you can set the value of restaurantString in onAppear:
Code Block
struct RestaurantPickerView: View {
//...
@State var restaurantOptions:Array = UserDefaults.standard.stringArray(forKey: "restaurantOptions") ?? []
@State var restaurantString: String = "" //Initialize with a dummy value
//...
var body: some View {
ZStack {
//...
}
.onAppear {
//Set the actual value in `onAppear`
restaurantString = restaurantOptions.joined(separator: "\n")
}
}
}



If you could show how restaurantString was used, I would have proposed some other way.

Oh, my apologies. In my previous code, optionString was supposed to be restaurantString. But it is simply used to display all options in a text box. Does that change how I should approach this in any way or not?




 In my previous code, optionString was supposed to be restaurantString.

Then this line (44):
Code Block
optionString = optionString + currentOption + "\n"

should be:
Code Block
optionString = optionString + "\n" + currentOption

NO?


Does that change how I should approach this in any way or not?

Yes, it changes the best way you go.
Cannot use instance member 'restaurantOptions' within property initializer; property initializers run before 'self' is available
 
 
Q