Post

Replies

Boosts

Views

Created

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 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)             }           }         }       }     }   } }
3
0
2.0k
Mar ’21