[英]Not sure how to monitor updated object and then get the result to be public in swift
所以我想要实现的是将 ifset() 值存储到“iftrue”中,即 (true/false),然后根据 bool 值显示一些图像/文本。
我遇到的问题是当 ifset() 被执行时,selectthree 将被清空到 [] 之后,然后 selectthree[0] 不存在,然后它崩溃并说“索引超出范围”。 不知道如何保存中间状态。 尝试了某种方式的willset 或didset。 还是没有达到我想要的。 也许有一些简单的方法可以做到,只是我不知道
var iftrue:Bool = false
var selectthree: Array<Card>=[]{
didSet{
if oldValue.count == 3{
iftrue = ifset()
}
}
}
mutating func chooseCard(_ card:Card){
if let chosenIndex = twelve.firstIndex(where:{$0.id == card.id}){
twelve[chosenIndex].select.toggle()
indexthree.append(chosenIndex)
selectthree.append(twelve[chosenIndex])
if selectthree.count == 3{
if ifset() {
twelve[indexthree[0]].isset=true
twelve[indexthree[1]].isset=true
twelve[indexthree[2]].isset=true
twelve=twelve.filter({$0.isset != true})
if deck.count>=0{
tempocard=tempocard.refill(from: deck)
twelve=twelve+tempocard
deck=Array(Set(deck).subtracting(tempocard))
}
}
selectthree=[]
indexthree=[]
}
}
}
mutating func ifset() -> Bool {
if checkColor(selectgroup:selectthree) && checkShape(selectgroup:selectthree) &&
checkNumber(selectgroup:selectthree) {
return true
}else{
return false
}
}
func checkShape(selectgroup:Array<Card>)->Bool{
if (selectgroup[0].content == selectgroup[1].content)&&(selectgroup[1].content == selectgroup[2].content){
return true
}else if (selectgroup[0].content != selectgroup[1].content)&&(selectgroup[1].content != selectgroup[2].content){
return true
}else{
return false
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.