0

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:

  1. Cast from 'NSManagedObjectContext?' to unrelated type 'NSArray' always fails
  2. 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?

Adrian
  • 16,233
  • 18
  • 112
  • 180

1 Answers1

1

The managed object context has nothing to do with an array. It cannot be "filled" with objects. It is simply an abstract class that gives you access to your data.

The way you get objects from your Core Data store is as follows: make a fetch request, optionally add filter and/or sorting to the request, fetch the objects.

let request = NSFetchRequest(entityName:"Note")
let result = managedObjectContext.executeFetchRequest(request error:nil) as [Note]

You can now either filter this array (with filteredArrayUsingPredicate after casting to NSArray) or you can add your predicate to the original fetch request (which should even be more efficient).

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • `let result = ...` generates this compiler error: `Cannot invoke 'executeFetchRequest' with an argument list of type '(NSFetchRequest)'` – Adrian Feb 24 '15 at 12:54
  • Sure, I forgot the error parameter - you could have figured this out yourself simply via autocompletion ;-). – Mundi Feb 24 '15 at 22:16
  • Thank you! I'm a month into this, so I greatly appreciate your insight. – Adrian Feb 24 '15 at 22:17
  • My instructor sent me back to the drawing board. He wanted something that uses CoreData from start-to-finish without arrays. I'm still working on it, but I've got pretty good start. Here's another way if anyone cares: [http://stackoverflow.com/questions/28708865/swift-uisearchcontroller-wired-up-in-core-data-project-app-runs-but-search-not](Core Data from start to finish) – Adrian Feb 25 '15 at 01:47
  • 2
    I described in my last sentence how to do that. Apple recommends in-memory filtering in some cases. Your instructor should read up on best practices. – Mundi Feb 25 '15 at 09:11