Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Why does dictionaryWithObjectsAndKeys require a cast?



I'm curious about this. I have this snippet of code (boiled down)

	NSNumber* foo = [NSNumber numberWithInt:7];
	NSNumber* bar = nil;
	
	NSDictionary* dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
		foo == nil ? [NSNull null] : foo, @"foo",
		bar == nil ? [NSNull null] : bar, @"bar",
		nil
	];

In my build settings, I tell it to treat all warnings as errors, and I
get this for those two lines with the ternary:
warning: comparison of distinct Objective-C types lacks a cast

This fixes it:

	NSDictionary* dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
		foo == nil ? [NSNull null] : (NSObject*) foo, @"foo",
		bar == nil ? [NSNull null] : (NSObject*) bar, @"bar",
		nil
	];

Note that I cannot explicitly cast to NSNumber* for some reason.

Now, I actually appear to be fine. Putting in the cast to NSObject*
there works just fine and the code behaves properly after that, so
unless someone tells me that it's horrible and I shouldn't I have no
problem.

But I'm curious - why is this required? What's going on here? I'm just
not seeing the issue, whatever it is.

-Jim....
_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.