Re: Why does dictionaryWithObjectsAndKeys require a cast?
Re: Why does dictionaryWithObjectsAndKeys require a cast?
- Subject: Re: Why does dictionaryWithObjectsAndKeys require a cast?
- From: "Tim Yates" <email@hidden>
- Date: Tue, 31 Jul 2007 21:24:57 +0100
I'm not 100% sure, but from my java knowledge, both sides of the ternary
have to be of the same type...
I'm guessing that NSNull and NSNumber have NSObject as their closest shared
parent class, so casting is required...
Hope that makes sense (and is right...I'm sure someone will be along to
point out why not if not)
Tim
On 7/31/07, Jim Thomason < email@hidden> wrote:
>
> 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
>
_______________________________________________
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