khaskheli_ayaz khaskheli_ayaz - 6 months ago 22
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
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)


` }

Answer

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

You need to do:

if let results = context.executeFetchRequest(fetchRequest2) as? [MyObjectType] {
      for object in results {
        //do thing
    }
  }
Comments