<分区>
我需要一些帮助。我想检查数组中有多少 boolean 值 (true
,false
)。如何仅使用 if 语句进行检查。
var voteArray: Bool = [true,false,true,false,true,true,false,false,true]
然后,将[true]
添加到其他数组,将[false]
添加到其他
<分区>
我需要一些帮助。我想检查数组中有多少 boolean 值 (true
,false
)。如何仅使用 if 语句进行检查。
var voteArray: Bool = [true,false,true,false,true,true,false,false,true]
然后,将[true]
添加到其他数组,将[false]
添加到其他
最佳答案
您可以为此过滤
您的数组。
var voteArray = [true,false,true,false,true,true,false,false,true]
let trueArray = voteArray.filter { $0 }
let falseArray = voteArray.filter { !$0 }
//If you want count also then simply access count property of both trueArray and falseArray
编辑:正如您在评论中提到的,您希望使用 for 循环
来处理此问题,我不知道您为什么要这样,但您已经这样问了你可以这样走。
var voteArray = [true,false,true,false,true,true,false,false,true]
var trueArray = [Bool]()
var falseArray = [Bool]()
for item in voteArray {
if item {
trueArray.append(item)
}
else {
falseArray.append(item)
}
}
//Or you can go with individual for loop for both true and false
//For true
for item in voteArray where item {
trueArray.append(item)
}
//For false
for item in voteArray where !item {
falseArray.append(item)
}
关于arrays - 如何检查数组中有多少真实项和多少虚假项(SWIFT3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42411906/