繁体   English   中英

如何使用.contains 与 SwiftUI 中的结构数组?

[英]How to use .contains with array of Struct in SwiftUI?

我正在尝试检查我的appearedNames数组是否已经包含我的currentPerson字符串,但不幸的是我收到了这个错误:无法转换'String' 类型的值? 到预期的参数类型 '(ColoredName) throws -> Bool' ...我应该如何将.contains与结构一起使用?

struct ColoredName: Identifiable {
    let id = UUID()
    var name: String
    var color: Color
}

struct ContentView: View {
    @State private var names = ["Steve", "Bill", "Jeff", "Elon"]
    @State private var appearedNames = [ColoredName]()
    @State private var currentPerson: String?
    
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink("Change view") {
                    SecondView(appearedNames: $appearedNames)
                }
                
                HStack {
                    Button {
                        if currentPerson != nil && !appearedNames.contains(currentPerson) // <-- I got the error here {
                            appearedNames.append(ColoredName(name: currentPerson ?? "", color: .red))
                        }
                        changePerson()
                    } label: {
                        Text(currentPerson ?? "Start")
                            .foregroundColor(currentPerson != nil ? .red : .black)
                            .font(.largeTitle)
                    }
                    
                    Button {
                        if currentPerson != nil {
                            appearedNames.append(ColoredName(name: currentPerson ?? "", color: .green))
                        }
                        changePerson()
                    } label: {
                        Text(currentPerson ?? "Start")
                            .foregroundColor(currentPerson != nil ? .green : .black)
                            .font(.largeTitle)
                    }
                }
            }
        }
    }
    func changePerson() {
        if names.count > 0 {
            let index = Int.random(in: 0..<names.count)
            currentPerson = names[index]
            names.remove(at: index)
        }
    }
}

struct SecondView: View {
    @Binding var appearedNames: [ColoredName]
    
    var body: some View {
        VStack {
            ForEach(appearedNames) { person in
                Text(person.name)
                    .foregroundColor(person.color)
                    .font(.largeTitle)
                    .bold()
            }
        }
    }
}

尝试这样的事情,您将数组中ColoredNamename与 String currentPerson (未包装)进行比较:

if currentPerson != nil && !appearedNames.contains(where: { $0.name == currentPerson!} )  {
      appearedNames.append(ColoredName(name: currentPerson ?? "", color: .red))
  }

或者,或者:

if let theName = currentPerson, !appearedNames.contains(where: { $0.name == theName} )  {
    appearedNames.append(ColoredName(name: theName, color: .red))
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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