0

This code works in an Xcode 12.5 playground on BigSur 11.2.3 on an M1 mac:

import UIKit

var Choice = Array(repeating: "" , count: 3)
Choice [0] = """
This is the land of Gaul
The people are tall
and the children are small
"""

Choice [1] = """
This is the land of Anglos
The people wear black clothes
and they never eat mangoes
"""

Choice [2] = """
This is the land of Hesperia
The people drink their vangueria
and never suffer hysteria
"""

print(Choice [1])

However, when it is in a swiftUI view like so:

import SwiftUI

struct ThirdCalcView: View {

    @State private var selectedChoice = 0
    
    var str1 = """
    This goes
    over multiple
    lines
    """
    
    
    var Choice = Array(repeating: "" , count: 3)
    Choice [0] = """
    This is the land of Gaul
    The people are tall
    and the children are small
    """

    Choice [1] = """
    This is the land of Anglos
    The people wear black clothes
    and they never eat mangoes
    """

    Choice [2] = """
    This is the land of Hesperia
    The people drink their vangueria
    and never suffer hysteria
    """
    

    var body: some View {
            Form {
                Section {
                    
                    Picker("Choice", selection: $selectedChoice, content: {
                        Text("France").tag(0)
                        Text("England").tag(1)
                        Text("Spain").tag(2)

                    })

                    
                     
                }//section
                
                Section {
                    Text("The mnemonic is:\(Choice[selectedChoice])")
                }//section
                
            } //form

    }//some view
    
} //ThirdCalcView

struct ThirdCalcView_Previews: PreviewProvider {
    static var previews: some View {
        ThirdCalcView()
    }
}

I get this array of errors after the 'var Choice' declaration:

enter image description here

I annotated the closing braces to make sure I didn't miscount them.

The simple 'str1' multiline declaration at the beginning is fine so I think my declaration of an array is causing the problem.

I want to display a string based on the user's choice. Ultimately in the app, the user has to make three selections before the response is displayed so I am looking at using an array with three dimensions.

1 Answer 1

1

You can't write code like that (meaning Choice[0] = ...) in the top level of a struct or class - it has to be either inside of a function or initializer.

There are a few options that you have. The most straightforward is just declaring the Choice on creation instead of assigning each index imperatively:

struct ThirdCalcView: View {

    @State private var selectedChoice = 0
    
    let Choice = [
    """
    This is the land of Gaul
    The people are tall
    and the children are small
    """,
    """
    This is the land of Anglos
    The people wear black clothes
    and they never eat mangoes
    """,
    """
    This is the land of Hesperia
    The people drink their vangueria
    and never suffer hysteria
    """]
    
    var body: some View {
      //...
    }
}

(Note: normally variable names are lowercased in Swift)

Another option would be to return the String from a switch statement:

struct ThirdCalcView: View {
    
    @State private var selectedChoice = 0
    
    func mnemonic() -> String {
        switch selectedChoice {
        case 0:
            return """
                This is the land of Gaul
                The people are tall
                and the children are small
                """
        case 1:
            return  """
                    This is the land of Anglos
                    The people wear black clothes
                    and they never eat mangoes
                    """
        case 2:
            return """
                This is the land of Hesperia
                The people drink their vangueria
                and never suffer hysteria
                """
        default:
            assertionFailure("Shouldn't reach here")
            return ""
        }
    }
    
    var body: some View {
        Form {
            Section {
                Picker("Choice", selection: $selectedChoice) {
                    Text("France").tag(0)
                    Text("England").tag(1)
                    Text("Spain").tag(2)
                }
            }//section
            
            Section {
                Text("The mnemonic is:\(mnemonic())")
            }//section
            
        } //form
        
    }//some view
    
} //ThirdCalcView

You could refactor this even further to make your selection based on an enum with the country names and then return a different mnemonic based on the enum value.

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

2 Comments

Thank you for helping me. As I am a beginner, I appreciate people who don't mind stating the obvious. Unfortunately I have 36 choices based on three parameters. That is going to be a LOOONG 'let' statement
You might want to consider something like storing all of the options in a JSON file or something like that and loading them dynamically. More work to set up, but possibly less work to maintain over time.

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.