你可以filter你的数组。
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 loop 来处理这个问题,我不知道你为什么要这样,但你已经问过了,所以你可以这样.
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)
}