khaskheli_ayaz khaskheli_ayaz - 2 years ago 157
iOS Question

Fetching From CoreData returns AnyObject

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
for result in (result2 as? NSDictionary)!{

if let data : NSDictionary = result as? NSDictionary{


Offer Entity contains No Of fields. Kindly Help. It Will Be Appreciated.

print("result: ",result)

` }

Answer Source

context.executeFetchRequest() returns [AnyObject], not AnyObject.

You need to do:

if let results = context.executeFetchRequest(fetchRequest2) as? [MyObjectType] {
      for object in results {
        //do thing
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download