Why doesn't this NSPredicate work? - cocoa

I have an Array of MKAnnotation objects called arrAnnotations. I want to pick out one of the annotations with the same coordinate as the one stored in a CLLocation object called "newLocation".
I'm trying to use a NSPredicate, but it doesn't work.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(SELF.coordinate == %f)", newLocation.coordinate];
NSArray* filteredArray = [arrAnnotations filteredArrayUsingPredicate:predicate];
[self.mapView selectAnnotation:[filteredArray objectAtIndex:0] animated:YES];
The filteredArray always contains zero objects.
I have also tried the following, which do not work either
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(coordinate == %f)", newLocation.coordinate];
and
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(coordinate > 0)"];
and
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(coordinate.latitude == %f)", newLocation.coordinate.latitude];
The last two crash the app, the third one with an NSInvalidArgumentException for [NSConcreteValue compare:] and the fourth, because latitude is not key-value-coding-compliant (I assume this is because coordinate is just a c-struct and not an NSObject?).
How can I make this work with NSPredicate?
Can anyone give me a link to a document that shows how Predicates work under the hood?
I don't understand what they actually do, even though I have read and understood most of Apple's Predicate Programming Guide.
Is searching a huge array with predicates more efficient than just looping through it with a for...in construct? If yes/no, why?

MKAnnotation protocol's coordinate property is a CLLocationCoordinate2D struct, thus it is not allowed in NSPredicate format syntax according to the Predicate Format String Syntax
You could use NSPredicate predicateWithBlock: instead to accomplish what you are trying to do, but you have be careful with CLLocationCoordinate2D and how to compare it for equality.
CLLocationCoordinate2D's latitude and longitude property are CLLocationDegrees data type, which is a double by definition.
With a quick search you can find several examples of problems you would face when comparing floating point values for equality. Some good examples can be found here, here and here.
Given all that, I believe that using code bellow for your predicate might solve your problem.
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
id<MKAnnotation> annotation = evaluatedObject;
if (fabsf([annotation coordinate].latitude - [newLocation coordinate].latitude) < 0.000001
&& fabsf([annotation coordinate].longitude - [newLocation coordinate].longitude) < 0.000001) {
return YES;
} else {
return NO;
}
}];

Related

NSTreeController - Retrieving selected node

I added Book object in bookController (NSCreeController). Now i want to get stored Book object when i select the row.
- (IBAction)addClicked:(id)sender {
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
// NSTimeInterval is defined as double
NSUInteger indexArr[] = {0,0};
Book *obj = [[Book alloc] init];
NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterLongStyle];
obj.title = [NSString stringWithFormat:#"New %#",dateString];
obj.filename = [NSString stringWithFormat:#"%d",arc4random()%100000];
[self.booksController insertObject:obj atArrangedObjectIndexPath:[NSIndexPath indexPathWithIndexes:indexArr length:2]];
}
I concede there perhaps could be a better solution--
I am unfamiliar with how NSTreeController works, but I looked a the class reference and noticed that it has a content property, similar to an NSArrayController (Which I am familiar with grabbing specific objects from).
I believe that if the content property is actually of type of some kind of tree data structure, my answer here probably won't work. The class reference says this about content:
The value of this property can be an array of objects, or a
single root object. The default value is nil. This property is
observable using key-value observing.
So this is what I historically have done with the expected results:
NSString *predicateString = [NSString stringWithFormat:NEVER_TRANSLATE(#"(filename == %#) AND (title == %#)"), #"FILENAME_ARGUMENT_HERE", #"TITLE_ARGUMENT_HERE"];
NSArray *matchingObjects = [[self content] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:predicateString]];
Then simply calling -objectAtIndex: will grab you your object. Note that the NSArray will be empty if the object doesn't exist, and if you have duplicate objects, there will be multiple objects in the array.
I also searched for an answer to your question, and found this SO thread:
Given model object, how to find index path in NSTreeController?
It looks pretty promising if my solution doesn't work, the author just steps through the tree and does an isEqual comparison.
If you could (if it's not too much trouble), leave a comment here to let me know what works for you, I'm actually curious :)

Filtering with NSPredicate - Data Type?

I wish to filter an array using a predicate but I'm being curved balled with the correct handling of data types.
First off, I have a list I want to filter with:
NSMutableArray *HospitalIDs = [[NSMutableArray alloc]init];
[HospitalIDs addObject: #"1"]; // Must be "id", int & NSInteger not accepted by XCode
[HospitalIDs addObject: #"2"]; // Must be "id", int & NSInteger not accepted by XCode
[HospitalIDs addObject: #"3"]; // Must be "id", int & NSInteger not accepted by XCode
Secondly, I have a list of objects that I want to filter using the array above:
(It's defined as NSMutableArray *HospitalObjects and is pre-populated with HospitalObjects)
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.HospitalID IN %#", HospitalIDs];
[HospitalObjects filterUsingPredicate:predicate];
NSLog(#"%i",[HospitalObjects count]);
//The expected result is 3, but it prints 0.
WHY?
Because by first array contains NSString values, but my HospitalObject.HospitalID is of type int.
I don't really want to change the data type of HospitalID in my object definition to NSString, but I cannot filter on integer values, what do I do?
Can I convert the data type WITHIN the predicate syntax? How?
First of all, the best way to store numbers in NSArray is to use NSNumber objects. You can simply convert a NSInteger to NSNumber using Objective-C literal syntax:
[HospitalIDs addObject: #(1)];
[HospitalIDs addObject: #(2)];
[HospitalIDs addObject: #(3)];
Then your predicate should work without any further changes.
There is a Type NSNumber that wraps ints, floats, bools, .. in a object.
NSMutableArray * hospitalIDs = [[NSMutableArray alloc] init];
[hospitalIDs addObject: #1]; // You can wrap an normal int into an NSNumber jusing #. In some siztuations you have to use #(yourOrdinaryValueTypeVariable)
[hospitalIDs addObject: #2];
[hospitalIDs addObject: #3];
By the way try to use proper naming using CamelCase:
Classes start with a upper case: SomeClassName
Objects, variables and method names with lower case: `SomeClassName* someObject;
Constants are all upper case: MY_CONSTANT

Fetch core data object where to-many relationship contains all objects in a set

What I'm looking for is a succinct way to fetch a core-data object that is identified uniquely by its relationships. e.g., given the data model:
... and given a set of NSManagedObject nodes, I'd like to fetch a triangle, or at least know if a TriangleEntity exists like so:
NSSet *nodeSet = // a set of 3 NSManagedObject*s
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:#"TriangleEntity"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"corners CONTAINS ALL %#", nodeSet];
I don't think that CONTAINS ALL is proper syntax -- And I know that I could just unpack the NSSet and fetch
#"corners CONTAINS %# AND corners CONTAINS %# AND corners CONTAINS %#", setObj1, setObj2, setObj3
... but that seems silly. Is there a more elegant solution?
The following predicate finds triangles where all corners are in the given set:
[NSPredicate predicateWithFormat:#"ALL corners IN %#", nodeSet]
nodeSet can be an NSSet or an NSArray.

avoid duplicate results on Core Data fetch

I have a subclass of the CoreDataTableViewController (subclass of UITAbleViewController dome by the people on Stanford done to link CoreData and TableViews). On this Class, I want to perform a fecth, sorting by an attribute called "definition" and the code which executes it is the following:
- (void)setupFetchedResultsController{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:self.entity];
request.propertiesToFetch=[NSArray arrayWithObject:#"definition"];
request.returnsDistinctResults=YES;
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:#"%K != nil", #"definition"];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:#"%K != ''", #"definition"];
NSPredicate *predicate3= [NSPredicate predicateWithFormat:#"%K contains[cd] %#", #"definition", self.seachBar.text];
NSArray *prepredicateArray;
if ([self.seachBar.text length]) {
prepredicateArray = [NSArray arrayWithObjects:predicate1, predicate2, predicate3,nil];
}else {
prepredicateArray = [NSArray arrayWithObjects:predicate1, predicate2,nil];
}
request.predicate=[NSCompoundPredicate andPredicateWithSubpredicates:prepredicateArray];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"definition" ascending:YES ]];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self performFetch];
}
If I understood it correctly, setting request.returnsDistinctResults=YES; should avoid fetching duplicates. However it doesn't work and I'm seeing duplicates of this attribute's value.
Is there something I'm missing there? I'd appreciate some pointings there. Thank you in advance.
EDIT: If anyone is having the same issue here, after applying David's answer the resulting fetchedResultsController is just a NSDIctionary with object with only the requested value, which for displaying only purposes is quite fine. One thing I've done in cellForRowAtIndexPath in order to display the results on the cell label is:
Before:
HNMR *hnmr = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text=hnmr.definition;
After:
cell.textLabel.text=[[self.fetchedResultsController objectAtIndexPath:indexPath] valueForKey:#"definition"];
From the documentation of returnsDistinctResults:
This value is only used if a value has been set for propertiesToFetch.
From the documentation of propertiesToFetch:
This value is only used if resultType is set to NSDictionaryResultType.
From the documentation of resultType:
The default value is NSManagedObjectResultType.
This all tells me that the propertiesToFetch is ignored because you haven't set the resultType yourself and the default it to return managed objects instead of dictionaries. Since the propertiesToFetch is ignored the returnsDistinctResults is ignored as well and thus you are still getting duplicates.
Try setting the result type to return dictionaries instead of managed objects.
request.resultType = NSDictionaryResultType;
In addition to David Rönnqvist answer I suggest a useful link (with a sample) on selecting distinct values with Core Data:
core-data-how-to-do-a-select-distinct
Hope that helps.

Cast an NSDictionary value while applying an NSPredicate?

I have an Array of NSDictionary objects.
These Dictionaries are parsed from a JSON file.
All value objects in the NSDictionary are of type NSString, one key is called "distanceInMeters".
I had planned on filtering these arrays using an NSPredicate, so I started out like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(distanceInMeters <= %f)", newValue];
NSArray *newArray = [oldArray filteredArrayUsingPredicate:predicate];
I believe this would have worked if the value for the "distanceInMeters" key was an NSNumber, but because I have it from a JSON file everything is NSStrings.
The above gives this error:****** -[NSCFNumber length]: unrecognized selector sent to instance 0x3936f00***
Which makes sense as I had just tried to treat an NSString as an NSNumber.
Is there a way to cast the values from the dictionary while they are being filtered, or maybe a completely different way of getting around this?
Hope someone can help me :)
Try this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(distanceInMeters.floatValue <= %f)", newValue];
Your problem isn't the predicate; your problem is that you're dealing with NSString objects instead of dealing with NSNumber objects. I would focus my time on changing them to NSNumbers first, and then verify that it's not working.
FYI, the JSON Framework does automatic parsing of numbers into NSNumbers...

Resources