• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Why does dictionaryWithObjectsAndKeys require a cast?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: Why does dictionaryWithObjectsAndKeys require a cast?
      • From: "Tim Yates" <email@hidden>
  • Prev by Date: Re: NSTableView crashing on scroll+update (SOLVED)
  • Next by Date: Developers required
  • Previous by thread: Re: CoreData min and max entity settings
  • Next by thread: Re: Why does dictionaryWithObjectsAndKeys require a cast?
  • Index(es):
    • Date
    • Thread