I'm brand new to Xcode and Swift and I'm running into some typing issues.
So I have a picker list in a view:
Picker(selection: $drinkType, label: Text("Drink Type")) {
ForEach(0 ..< drinks.count) {
Text(self.drinks[$0]).tag($0)
}
And I want to have it converted into text so I can display it on a text box, so I need to convert it into a String. My function which takes in these parameters and outputs them is below:
private func addDrink() {
let drink = Drink(context: context)
drink.dateDrank = Date()
drink.drinkType = drinks
do {
try context.save()
presentationMode.wrappedValue.dismiss()
} catch let err {
print(err.localizedDescription)
}
}
The issue arises with the drinks variable, it is currently an array you cycle through with the picker:
var drinks = ["Pint", "Spirit", "Shot", "Cocktail"]
The error it gives me currently is: Cannot Assign type [String] to Type String
And when I try to add a String cast: drink.drinkType = String(drinks)
The error then thrown my way is: No exact matches in call to initialiser and it gives me no pointers to what that could be/mean.
Thanks in advance
Update
Inside the Picker I added a new variable to store the result:
Picker(selection: $drinkType, label: Text("Drink Type")) {
ForEach(0 ..< type.count) {
Text(self.type[$0]).tag($0)
AddNewDrink.typeSelected = self.type[$0]
}
So the new function looks like:
private func addDrink() {
let drink = Drink(context: context)
drink.dateDrank = Date()
drink.drinkType = AddNewDrink.typeSelected
do {
try context.save()
presentationMode.wrappedValue.dismiss()
} catch let err {
print(err.localizedDescription)
}
}
}
However the issue now thrown my way is Type '()' cannot conform to 'View' on the ForEach Loop
drink.drinkType = String(drinks[0])or some other valuedrinkType