简体   繁体   中英

Declaration error when using an array in a swiftUI struct

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:

在此处输入图像描述

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM