2
如何在For/In循环中实现完成处理程序? 我有两个CNLabeledContact的阵列称为phonesArray:For/In循环中的完成处理程序 - swift
变种myPhoneNumberArray = CNLabeledValue
for item in phonesArray {
let phonesArrayValue = item.value as! CNPhoneNumber
let phonesArrayValueDigits = phonesArrayValue.valueForKey("digits")!
print("current value: \(phonesArrayValueDigits)") //
DataService.dataService.checkIfPhoneExistsInDatabase("\(phonesArrayValueDigits)") { (bool) in
if bool {
print("append this item")
self.myPhoneNumberArray.append(item)
}
else {
}
}
}
print("My phonenumbers array is:")
print(myPhoneNumberArray)
此,跑步,打印:
current value: 37439
current value: 78735
My phonenumbers array is:
[]
append this item //Only the second number matches the database and is appenned
我想:
current value: 37439
Current value: 78735
append this item
[<CNLabeledValue:....digits=78735>>]
我的猜测是'checkIfPhoneExistsInDatabase'是异步的,所以直到后来才打印结果。如果你想正确打印,请将'print(“当前值:\(phonesArrayValueDigits)”)'移到'if bool {'上方。 – erdekhayser
你是对的,但我简化了我的问题太多。我将编辑我的问题。 – standousset