I'm relatively new to iOS and I find myself "thrown to the wolves" on a Core Data assignment. I'm trying to create an array of Note
objects using a NSPredicate
to search a managedObjectContext
. It's not working, but I think I'm close to the "finish line". Here's what I've got:
I have a managedObjectContext
instantiated and there are Note
objects in it.
I have a variable I created called filteredNotes
at the top of the class that's instantiated empty (at least I think so).
var filteredNotes: [Note] = []
Further down, I have a function that I want to "fill up" the filteredNote array with note objects that match a predicate. Here's how I'm trying to do it:
let resultPredicate = NSPredicate(format: "noteTitle contains[c] %@", searchBar.text)
filteredNotes = (managedObjectContext as! NSArray).filteredArrayUsingPredicate(resultPredicate)
The compiler has this to say about it:
Cast from 'NSManagedObjectContext?' to unrelated type 'NSArray' always fails
Cannot assign a value of type '[AnyObject]' to a value of type '[Note]'
I see a lot of examples of vanilla NSArrays
(not managedObjectContexts
) doing this and I'm stumped why I can't do it w/ a managedObjectContext
that's filled with Note
objects.
Here's what my Note: NSManagedObject looks like:
import Foundation
import CoreData
class Note: NSManagedObject {
@NSManaged var dateCreated: NSDate
@NSManaged var dateEdited: NSDate
@NSManaged var noteTitle: String
@NSManaged var noteBody: String
}
Any suggestions on how to create the filteredNote
array?