首页 > 解决方案 > 在 swiftUI 结构中使用数组时出现声明错误

问题描述

此代码适用于 M1 mac 上 BigSur 11.2.3 上的 Xcode 12.5 操场:

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])

但是,当它在这样的 swiftUI 视图中时:

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()
    }
}

在“var Choice”声明之后,我得到了一系列错误:

在此处输入图像描述

我注释了右括号,以确保我没有记错它们。

开头的简单“str1”多行声明很好,所以我认为我的数组声明导致了问题。

我想根据用户的选择显示一个字符串。最终在应用程序中,用户必须在显示响应之前进行三个选择,所以我正在研究使用具有三个维度的数组。

标签: arraysswiftui

解决方案


您不能在 a orChoice[0] = ...的顶层编写这样的代码(意思是 )- 它必须在函数或初始化程序内部。structclass

您有几个选择。最直接的方法就是声明Choiceon 创建,而不是命令式地分配每个索引:

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 {
      //...
    }
}

(注意:通常变量名在 Swift 中是小写的)

另一种选择是String从 switch 语句返回:

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

您可以进一步重构它以根据enum国家名称进行选择,然后根据枚举值返回不同的助记符。


推荐阅读