Reputation: 2057
I am Fetching Entity Data From coreData, That returns AnyObject, I tried A lot to convert in NSDictionary and NSArray but it Can not cast any type.`
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)
}
Offer Entity contains No Of fields. Kindly Help. It Will Be Appreciated.
print("result: ",result)
print("dictData:",dictData)
` }
Upvotes: 3
Views: 917
Reputation: 285064
According to its signature executeFetchRequest
returns always an array of AnyObject
. AnyObject, because the return value can be NSManagedObject
or Dictionary
depending on the fetch request.
The default return value is a non-optional [NSManagedObject]
so write
let result2 = try context.executeFetchRequest(fetchRequest2) as! [NSManagedObject]
the forced unwrapping of the type is safe is this case, there's no need of optional bindings.
If you are using a subclass of NSManagedObject
you can even cast the return value to that type.
Upvotes: 1
Reputation: 5188
context.executeFetchRequest()
returns [AnyObject]
, not AnyObject
.
You need to do:
if let results = context.executeFetchRequest(fetchRequest2) as? [MyObjectType] {
for object in results {
//do thing
}
}
Upvotes: 5