views:

672

answers:

2

Why does this code...:

NSDictionary *testDictionary = [NSDictionary dictionaryWithObjectsAndKeys:kABOtherLabel, @"other", kABWorkLabel, @"work", nil];
// There are 9 more key-value pairs that i've omitted here.

throw this warning:
warning: passing argument 1 of 'dictionaryWithObjectsAndKeys' from incompatible pointer type

Incidentally, the code works as expected, but I don't like leaving warnings un-delt-with. I assume it doesn't like that I'm storing a constant in a dictionary. Well, where can I store it then? Should I just place (void *) before every constant?

+1  A: 

I believe that kABOtherLabel is a constant integer which is not an object. If you want to add it as an object use something along with lines of [NSNumber numberWithInteger:kABOtherLabel] (same goes for second value object)

Eimantas
I see, so it wants an object. Well, if you print `kABOtherLabel` you get a value of `_$!<Other>!$_` so I don't think it's an integer, it's a `CFTypeRef`. BUT it sounds like you're right that it's not an object and that's what `NSDictionary` is looking for. I wonder what the right way is to store a list of String > Constant pairs?
Andrew
Well regarding the printing you can print with %d placeholder which will show you integer representation of that constant. Other than that - you can store string > constant pairs in old-school struct.
Eimantas
+1 Oh, I like that. Maybe I'll make a dictionary of string > integer pairs.
Andrew
+1  A: 

On the iPhone, kABOtherLabel is a CFStringRef and not an NSString *. However, the two are toll-free bridged, so you can just cast it to NSString *:

NSDictionary *testDictionary = [NSDictionary dictionaryWithObjectsAndKeys:(NSString *)kABOtherLabel, @"other", (NSString *)kABWorkLabel, @"work", nil];

(Also, you may have your keys and values reversed in this call, unless you want your literal strings to be the keys (objects come first)).

Wevah
+1 Thanks. Good plan. I noticed that I only get the warning as it relates to that first object. Typecasting the other objects doesn't seem necessary (as it pertains to warnings, at least). I DO want my literal strings to be the keys. It took me a minute to discover the order of things.
Andrew