i have this Core Data object
class Foo: NSManagedObject {
@NSManaged var YearRange: AnyObject? // [2000,2015]
}
i need to know if 2005 in in the range of the NSManagedObject property YearRange.
this is my fetch:
let request :NSFetchRequest = NSFetchRequest(entityName:"Foo")
let Predicate = NSPredicate(format: "xxx", "xxx")
request.predicate = Predicate
var error: NSError? = nil
var matches: AnyObject = self.managedObjectContext!.executeFetchRequest(request, error: &error)!
if (error == nil && matches.count > 0){
println(matches)
}
what is the NSPredicate format that can help me ?
Since CoreData does not natively support array attributes, I assume
YearRange
is defined as a Transformable. If that is the case, and assuming you are using a SQLLite store, you cannot use a predicate based onYearRange
to filter the fetch request. (Transformables are stored in the SQL database as NSData blobs, and are only transformed back into the appropriate object types when loaded into memory. For the SQLLite store, CoreData compiles any predicate into the fetch SQL and evaluates the result in the database itself. Hence the predicate cannot be based on Transformables.)You have two options: 1) you could fetch all the objects, and then filter them in memory, or 2) amend your model to store the
YearRange
as CoreData native types (ie. strings, numbers, dates, etc).If the elements of the
YearRange
array represent the start and end years of a range, then I recommend the latter option, with two integer attributes:yearRangeStart
andyearRangeEnd
. IfYearRange
might contain several discrete years, then the first option is probably best.