0

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

5
  • you are trying to cast an array to string. That produces the error. Try using drink.drinkType = String(drinks[0]) or some other value Commented Feb 20, 2022 at 11:58
  • Certainly you want to assign one type, not the entire array. And probably you want to assign the selected type which is drinkType Commented Feb 20, 2022 at 12:00
  • @udi I'll try that, although passing in the index gives me a stationary value for the variable at that index. I tried $0 for the movable index but it didn't work. Commented Feb 20, 2022 at 12:25
  • @vadian I tried that, assigning drinkType actually assigns the index value, i.e. 0-3 Commented Feb 20, 2022 at 12:25
  • Please see the answer. Commented Feb 20, 2022 at 12:31

1 Answer 1

1

Create the picker to iterate the strings rather than the indices

Picker(selection: $drinkType, label: Text("Drink Type")) {
    ForEach(drinks, id: \.self) { type in 
        Text(type)
    }
}

And drinkType this way

@State private var drinkType = "Pint"

Then assign the selected type

drink.drinkType = drinkType

Side note: drinks for the type array is misleading. A better name is for example just types

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I see where it's going but @State private var drinkType = "Pint" instead of an array it's not going to iterate through anything as your just assigning it a string. What would then be point of the picker if you can only choose the one option? It would fix the casting issue but there wouldn't be any other option to choose
Declaring drinkType in Core Data as String attribute implies that there is only one type. "Pint" is the default value. If you choose another type from the picker the @State property is updated accordingly due to the selection Binding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.