Search code examples
objective-ciosios5

iOS ARC: OR operator on ManagedObject pointers


I'm working with NSManagedObjects and I'd like to return either the tasks's budget or the task's category's budget.

However, the method gives the error:

Implicit conversion of int to Budget * is disallowed with ARC.

What's going on here?

@implementation Task

@dynamic category;
@dynamic budget;

- (Budget *)budgetOrCategoryBudget {
    return [self budget] || [[self category] budget];
}

Solution

  • || is an operator that takes two objects or primitives and returns TRUE if at least one of the values is non-nil (if an object) or non-zero (if a primitive) and FALSE otherwise. In Objective-C BOOL is actually an int, where FALSE is 0 and TRUE is anything non-zero. So you are actually returning a BOOL (represented by an int) where the compiler is expecting a Budget *.

    If you want to actually return one of the two budgets, you must provide some other means of selecting between them.