Creating & implementing functions in SwiftUI errors

Viewed 26

I'm trying to create a program that takes two values from pickers and returns a string. The code below has two errors that I was hoping someone could help me fix. I'm quite inexperienced in swift development so I apologise if the code appears crude

Errors:

In the if statement, combination has the error: Cannot assign to property: 'self' is immutable

In the preview provider DrugCombinationPicker() has the error: Missing argument for parameter 'combination' in call

Code:

import SwiftUI
struct DrugCombinationPicker: View {
    @Environment(\.colorScheme) var colorScheme
    @State var drug1Value: String = ""
    @State var drug2Value: String = ""
    var combination: String
    let drugs: [String] = ["Shrooms", "Cannabis", "MDMA", "Cocaine", "Caffeine", "Alcohol", "Benzodiazephines", "Opiods"]
    var body: some View {
        VStack{
            Text("Pick two drug combinations")
            HStack{
                Picker("Drug One", selection: $drug1Value){
                    ForEach(drugs, id: \.self){ drug1 in
                        Text(drug1)
                }
            }
            Spacer()
            }
            HStack{
                Picker("Drug One", selection: $drug2Value){
                    ForEach(drugs, id: \.self){ drug2 in
                        Text(drug2)
                    }
                }
            }
            HStack{
                Text("Your selected combination is \(returnCombo())" )
            }
        }
    }

    func returnCombo() -> String {
        var choice1 = drugs.firstIndex(where: {$0 == drug1Value})!
        var choice2 = drugs.firstIndex(where: {$0 == drug2Value})!
        //0 = No interaction
        //1 = Safe & No Synergy
        //2 = Safe & Synergy
        //3 = Unsafe
        //4 = Safe/Decreases high
        //5 = Deadly
        let matrix = [
        [0,2,2,1,1,1,4,1],
        [2,0,2,1,1,1,4,2],
        [2,2,0,3,3,3,4,3],
        [1,1,3,0,3,3,4,5],
        [1,1,3,3,0,1,4,1],
        [1,1,3,3,1,0,5,5],
        [4,4,4,4,4,5,0,5],
        [1,2,3,5,1,5,5,0]
        ]
        let decision = matrix[choice1][choice2]
        if (decision == 0){
            combination = "No interaction"
        }
        else if (decision == 1){
            combination = "Safe & No Synergy"
        }
        else if (decision == 2){
            combination = "Safe & Synergy"
        }
        else if (decision == 3){
            combination = "Unsafe"
        }
        else if (decision == 4){
            combination = "Safe/Decreases high"
        }
        else if (decision == 5){
            combination = "Deadly"
        }
        return combination
    }
}
struct DrugCombinationPicker_Previews: PreviewProvider {
            static var previews: some View {
            DrugCombinationPicker()
        }
    }
1 Answers

You don't need it as a member of view (and View is read-only anyway), make it local, like

func returnCombo() -> String {
   var combination: String         // << here !!
Related