我正在从 coreData 获取实体数据,它返回 AnyObject,我尝试了很多在 NSDictionary 和 NSArray 中进行转换,但它无法转换任何类型。`
var dictData:NSDictionary?
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context = appDelegate.managedObjectContext;
// self.selectedArray = response.objectForKey("retailers") as! NSArray;
let fetchRequest2 = NSFetchRequest()
let entityDescription2 = NSEntityDescription.entityForName("Offers", inManagedObjectContext: context)
fetchRequest2.entity = entityDescription2
//fetchRequest2.returnsObjectsAsFaults = false
do {
let result2 : NSDictionary = try context.executeFetchRequest(fetchRequest2) as! NSDictionary
print("Result:",result2)
for result in (result2 as? NSDictionary)!{
if let data : NSDictionary = result as? NSDictionary{
print(data)
}
报价实体包含 No Of 字段。请帮助。它会受到赞赏。
print("result: ",result)
print("dictData:",dictData)
` }
最佳答案
context.executeFetchRequest()
返回 [AnyObject]
,而不是 AnyObject
。
你需要做:
if let results = context.executeFetchRequest(fetchRequest2) as? [MyObjectType] {
for object in results {
//do thing
}
}
关于ios - 从 CoreData 获取返回 AnyObject,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37582746/