0

I'm building a game and have a spell class. Within this class there's a list of active instances of this spell. I'm trying to check if this spell has already been cast on some target by using a predicate: target == %@ . However, the code below does not return any objects.

How can I check if a key is equal to a custom object within a predicate?

   -(BOOL)checkHasUniqueInstanceWithModel:(CharacterModelNode*)targetModel
    {
        NSPredicate *uniqueSkillInstancePredicate =
 [NSPredicate predicateWithFormat:@"target == %@", targetModel];



        NSArray *results = [self.activeInstances filteredArrayUsingPredicate:uniqueSkillInstancePredicate];

        if(results.count == 0)
        {
            return NO;
        }else if(results.count == 1)
        {
            return YES;
        }else
        {
            NSAssert(false,@"Duplicate unique instance with skill: %@ on target: %@",self.name,targetModel.character.name);
        }
        return NO;
    }
1
  • found that the predicate was correct, I was simply comparing it to a wrong object. Commented Jan 25, 2014 at 1:29

1 Answer 1

1

Use a predicate with block:

NSPredicate *pred = [NSPredicate predicateWithBlock:^BOOL(Spell* evaluatedObject, NSDictionary *bindings) {
    if([evaluatedObject.target isEqual: targetModel])
    {
        return true;
    }
    return false;
}];

NSArray *results = [self.activeInstances filteredArrayUsingPredicate:pred ];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.