Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
I have an NSSet of NSManagedObjects whose subclass has a property called "level". The NSSet is supposed to be filtered by "level".
The user finishes a level, then the app checks if an NSManagedObject exists for the next level (indicating that the user had already unlocked that level). It does this by calling a method that filters the set using a predicate and returns the result of a call to "anyObject".
Note: levels are numbered starting with 0.
Here's my code:
Code:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"level==%f",level];

    NSSet *filteredSet = [self.places filteredSetUsingPredicate:predicate];

    NSManagedObject *objectToReturn = [filteredSet anyObject];
However, my debugger indicates that objectToReturn is set by anyObject to the NSManagedObject that corresponds to the first level. Therefore, even when level=1, objectToReturn != nil, meaning the aforementioned check does not fail.

Is filteredSetUsingPredicate supposed to return anything, even if nothing in the set meets the predicate?
 
Is level a float? (%f is for floating point)

filteredSetUsingPredicate should return an empty set if no objects match the predicate.

anyObject returns nil for an empty set.

The debugger isn't always right. You might want to NSLog the value to be sure.

Also, looking at the Apple docs and using unit tests and log statements would help you to understand this better.
 
Is level a float? (%f is for floating point)

filteredSetUsingPredicate should return an empty set if no objects match the predicate.

anyObject returns nil for an empty set.

The debugger isn't always right. You might want to NSLog the value to be sure.

Also, looking at the Apple docs and using unit tests and log statements would help you to understand this better.

Oh, ok. Thanks for the advice. Edit: Oh, and level is an int.
 
This NSLog statement appears to be producing the same result:
Code:
NSLog(@"PlaceEntity objectToReturn: %@", objectToReturn);

Edit: At least, in the simulator. The previous run was on my device.
 
So, I switched to using this code:
Code:
NSSet *filteredSet = [self.places objectsPassingTest:^BOOL(NSManagedObject *obj, BOOL *stop)

                          {

                              return obj.level == level;

                          }];
which, for the same test case that I did above, causes NSLog to output "(null)".
 
OK, so I created a new user, jumped to the part where a new level would be unlocked, "unlocked" the new level. With the debugger, I verified that the level on the newly created NSManagedObject has the correct level. Just now, I closed the app and re-ran it. It seems like the problem should no longer exist.
[doublepost=1456972680][/doublepost]OK, deleted app, re-ran, created new user...works again. (Of course, I did stop the app before the part where the app would unlock a new level)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.