Alex Rivera | Logout

Objective-C: Result of casting id to BOOL?

Asked 2011-08-19T15:18:14.327
13

Does the following function return YES if object != nil?

- (BOOL)boolForObject:(id)object {
    return (BOOL)object;
}

I've tested with object = [[NSObject alloc] init] but got mixed results.

Edit
Report

2 Answers

7

What is the actual purpose of your method?

If it's just to check whether an object is nil or not, why not do it like this:

- (BOOL)boolForObject:(id)object 
{
    return (object != nil);
}

It's more obvious what the result will be.

answered 2011-08-19T15:22:44.373
5

You could also do:

return !!object;
answered 2011-10-19T14:50:03.460

Your Answer