[英]Swift Equatable Struct doesn't allow updating?
我试图理解 Equatable。 当我在我的 CreateCustomer 结构上使用Equatable
时,如果我设置了一个,为什么我不能添加更多电话类型,或者当我添加了更多电话类型时,为什么我只能设置一个? 在我的结构上没有Equatable
它工作正常。
这是我的 SwiftUI 视图来设置电话类型
struct T01: View{
@State var phoneTypes: [String] = ["Other", "Home", "Service", "Work", "Cell"]
@State var customerCreate: CreateCustomer = CreateCustomer()
var body: some View {
VStack{
if (customerCreate != CreateCustomer()){
Button(action: {
customerCreate = CreateCustomer()
}, label: {
Text("Clear").padding()
})
}
ForEach($customerCreate.phone.indices, id: \.self) { i in
Menu {
ForEach(phoneTypes, id: \.self){ client in
Button() {
let x = client
customerCreate.phone[i].phoneType = x
print(customerCreate.phone[i].phoneType)
} label:{
Text(client)
if customerCreate.phone[i].phoneType == client
{
Image(systemName: "checkmark")
}
}
}
} label: {
VStack{
HStack{
Spacer()
Text(customerCreate.phone[i].phoneType.isEmpty ? "Select the phone type *" : customerCreate.phone[i].phoneType)
.foregroundColor(customerCreate.phone[i].phoneType.isEmpty ? .gray : .black)
Image(systemName: "chevron.down")
.foregroundColor(Color.green)
Spacer()
}
}
}
}
Button(action: {
customerCreate.addPhone()
}, label: {
HStack {
Image(systemName: "plus.circle")
.font(.system(size: 15))
Text("Add Phone")
.fontWeight(.thin)
.font(.system(size: 15))
}
})
}
}
}
struct CreateCustomer: Codable, Equatable {
static func == (lhs: CreateCustomer, rhs: CreateCustomer) -> Bool {
// It can be fixed by changing == to > but I want the == so I can know if I should display the clear button or not.
return String(lhs.phone.first?.phoneType ?? "") == String(rhs.phone.first?.phoneType ?? "")
}
var phone: [CustomerPhone]
init() {
phone = [CustomerPhone()]
}
public mutating func addPhone(){
phone.append(CustomerPhone())
}
}
struct CustomerPhone: Codable {
var phone: String
var phoneType: String
init(){
phone = ""
phoneType = ""
}
}
谢谢你的帮助!!!!
通过一些整理和重命名,并使Customer
成为ObservableObject
,我认为这可以满足您的需求……
struct ContentView: View {
@State var phoneTypes: [String] = ["Other", "Home", "Service", "Work", "Cell"]
@StateObject var customer = Customer()
var body: some View {
VStack{
if !customer.isEmpty {
Button(action: {
customer.phones = []
}, label: {
Text("Clear").padding()
})
}
ForEach($customer.phones.indices, id: \.self) { i in
Menu {
ForEach(phoneTypes, id: \.self) { phoneType in
Button() {
customer.phones[i].phoneType = phoneType
print(customer.phones)
} label:{
Text(phoneType)
if customer.phones[i].phoneType == phoneType {
Image(systemName: "checkmark")
}
}
}
} label: {
VStack{
HStack{
Spacer()
Text(customer.phones[i].phoneType.isEmpty ? "Select the phone type *" : customer.phones[i].phoneType)
.foregroundColor(customer.phones[i].phoneType.isEmpty ? .gray : .black)
Image(systemName: "chevron.down")
.foregroundColor(Color.green)
Spacer()
}
}
}
}
Button(action: {
customer.addPhone()
print(customer.phones)
}, label: {
HStack {
Image(systemName: "plus.circle")
Text("Add Phone")
.fontWeight(.thin)
}
})
.font(.system(size: 15))
}
}
}
final class Customer: ObservableObject {
@Published var phones: [CustomerPhone]
var isEmpty: Bool {
phones == [CustomerPhone()]
}
init() {
phones = [CustomerPhone()]
}
public func addPhone(){
phones.append(CustomerPhone())
}
}
这就是我将如何实现 Customer 和 CustomerPhone 以及如何检查 Customer object 是否为空
struct Customer: Codable, Equatable {
var phones: [CustomerPhone]
init() {
phones = [CustomerPhone()]
}
public mutating func add(phone: CustomerPhone = CustomerPhone()){
phones.append(phone)
}
var isEmpty: Bool {
phones.isEmpty || phones.allSatisfy(\.isEmpty)
}
}
struct CustomerPhone: Codable, Equatable {
var phone: String
var phoneType: String
init(){
phone = ""
phoneType = ""
}
var isEmpty: Bool {
phone.isEmpty && phoneType.isEmpty
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.