NSSet filteredSetUsingPredicate returning invalid object

Discussion in 'iOS Programming' started by moonman239, Feb 25, 2016.

  1. moonman239 macrumors 68000

    Mar 27, 2009
    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:
    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?
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    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.
  3. moonman239 thread starter macrumors 68000

    Mar 27, 2009
    Oh, ok. Thanks for the advice. Edit: Oh, and level is an int.
  4. moonman239 thread starter macrumors 68000

    Mar 27, 2009
    This NSLog statement appears to be producing the same result:
    NSLog(@"PlaceEntity objectToReturn: %@", objectToReturn);
    Edit: At least, in the simulator. The previous run was on my device.
  5. moonman239 thread starter macrumors 68000

    Mar 27, 2009
    So, I switched to using this 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)".
  6. moonman239 thread starter macrumors 68000

    Mar 27, 2009
    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.
    --- Post Merged, Mar 2, 2016 ---
    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)

Share This Page