Why does dictionaryWithObjectsAndKeys require a cast?
Why does dictionaryWithObjectsAndKeys require a cast?
- Subject: Why does dictionaryWithObjectsAndKeys require a cast?
- From: "Jim Thomason" <email@hidden>
- Date: Tue, 31 Jul 2007 13:57:11 -0500
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:
This email sent to email@hidden