Neither is more correct.

The main distinction is whether you are happy with C's (very weak) type system 
or not.

If you are satisfied that if simply checks if some integer typed value is equal 
to 0 or not, and uses the first branch if it isn't, then the former is more 
concise and "better" for you.

If you on the other hand like to hold a stronger type system than C's in your 
head, and like to think of if statements as things that accept boolean 
arguments, and execute the first branch if the boolean is true, then clearly if 
(self) is an "in head" type error - you need to compare against nil to get a 
boolean out.

Hope that clarifies things.

Bob
if (*ra4 != 0xffc78948) { return false; }

On 24 Feb 2012, at 14:50, Oleg Krupnov wrote:

> An interesting question. The following samples are equivalent in terms
> of compiled code, but which one is more correct from the language's
> point of view?
> 
> self = [super init];
> if (self)
> {
> }
> return self;
> 
> self = [super init];
> if (self != nil)
> {
> }
> return self;
> 
> The Xcode samples promote the first variant, but I'm wondering if the
> second one is more correct?
> 
> The "nil" is defined as follows (jumped to definition)
> 
> #define nil NULL
> #define NULL ((void*)0)
> 
> So basically, nil is of type "void*", so the expression "self != nil"
> compares two pointers and the result is "boolean", which is perfect
> for testing in the "if" statement. But the "self" alone is of type
> "pointer" and so when it is tested by the "if" statement, it's
> implicitly cast to the type "boolean".
> 
> I also heard that generally speaking NULL is not necessarily always
> equal to 0 on all architectures.
> 
> Thoughts?
> _______________________________________________
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/tom.davie%40gmail.com
> 
> This email sent to tom.da...@gmail.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to