1
votes

It's the first time I'm trying to save and retrieve custom data to/from my core data, but I've run into an error saying:

fatal error: array cannot be bridged from Objective-C

when I try to load the data back.

My code looks like this, the arrayOfNames is declared as [String]:

@IBAction func saveTap(sender: AnyObject) {

    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let contxt: NSManagedObjectContext = appDel.managedObjectContext!
    let en = NSEntityDescription.entityForName("Indexes", inManagedObjectContext: contxt)


    let arrayData: NSData = NSKeyedArchiver.archivedDataWithRootObject(arrayOfNames)

    let newIndex = Indexes(entity: en!, insertIntoManagedObjectContext: contxt)
    newIndex.monday = arrayData

    println(newIndex.monday)

    contxt.save(nil)

}

@IBAction func loadTap(sender: AnyObject) {

    let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext!

    let fetchReq = NSFetchRequest(entityName: "Indexes")
    let en = NSEntityDescription.entityForName("Indexes", inManagedObjectContext: context)

    var myList:[String] = context.executeFetchRequest(fetchReq, error: nil) as! [String]

    println(myList)

}

My model-file looks like this:

@objc(Indexes)
class Indexes: NSManagedObject {
    @NSManaged var monday: NSData
    @NSManaged var tuesday: NSData
    @NSManaged var wednesday: NSData
    @NSManaged var thursday: NSData
    @NSManaged var friday: NSData
}

I've also set all the attributes to transformable in my data model. As I said, it's the first time I'm doing this, so sorry if the solution is obvious.

Any suggestions would be appreciated.

1

1 Answers

0
votes

executeFetchRequest returns [AnyObject]?, not [String], indeed the objects in the array should be Indexes instances which contain your archived arrays of strings.

So, you need to correct the array type that the results of the fetch are being placed into.