arled
arled

Reputation: 2690

populate NSMutableArray using the CoreData database

Im trying to populate an NSMutableArray from the CoreData database (using swift) but not sure what I'm doing wrong. Here's my code:

var stuff: NSMutableArray = []

populating the NSMutableArray:

override func viewDidAppear(animated: Bool) {
    let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext

    let freq = NSFetchRequest(entityName: "MyData")       
    stuff = context.executeFetchRequest(freq, error: nil)

    tableView.reloadData()

}

Error:

enter image description here

Upvotes: 2

Views: 454

Answers (1)

GoZoner
GoZoner

Reputation: 70135

Just define stuff as

var stuff : Array<AnyObject> = []

Which will create stuff as an array suitable for the return type of executeFetchRequest().

When you then access elements of the array you will also need to satisfy the type constraints. You should look into the Apple documentation for as, as? and is and the associated usage examples. In the very simplest case it would be something like:

var aThing : NSManagedObject? = stuff[0] as? NSManagedObject

For the above aThing would be nil if stuff[0] is not an NSManagedObject nor subclasses.

Upvotes: 4

Related Questions