我試圖將一個獲取請求的結果放入數組中。我的代碼:iOS核心數據:將獲取請求的結果轉換爲數組
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "CLIENTS")
var mobClients = [NSManagedObject]()
var arrayAllPhoneNumbers = [String]()
do {
let results = try managedContext.executeFetchRequest(fetchRequest)
mobClients = results as! [NSManagedObject]
for clientPhoneNumber in mobClients {
let myClientPhoneNumber = clientPhoneNumber.valueForKey("clientsMobilePhoneNumber") as! String
print(myClientPhoneNumber)
//The numbers print out just fine, one below the other
//
//Now the results need to go into the array I've declared above ---> arrayAllPhoneNumbers
messageVC.recipients = arrayAllPhoneNumbers // Optionally add some tel numbers
}
} catch
let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
如圖所示,所有電話號碼都需要在數組中捕獲。我如何實現這一目標?
創建表示核心數據實體的NSManagedObject子類可以更好地服務您。此外,如果您只需要電話號碼,則應在初始提取中使用謂詞,然後可以直接將結果數組分配給電話號碼。 –
對於像我這樣的n00b,這是很多(壓倒性的)信息!給我一兩天谷歌所有的,如果你不介意,我想回到你這個。同時感謝您的迴應! –