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