views:

114

answers:

1

Sorry if the question isn't correct, I'm very new in Objective-C.
I understand why this code throw the Warning: "warning: passing argument 1 of 'initWithObjectsAndKeys:' makes pointer from integer without"

NSDictionary *dictNames =
[[NSDictionary alloc] initWithObjectsAndKeys:
     3, @"",
     4, @"",
     5, @"",nil];

Keys and Values of a NSDictionary must be NSObject and not fundamental types, like the integers 3, 4 and 5. (Correct me if necessary).
But I don't understand why this warning dissapears with the only "correct typing" of the first Key.

NSDictionary *dictNames =
    [[NSDictionary alloc] initWithObjectsAndKeys:
     [NSNumber numberWithInteger:3], @"",
     4, @"",
     5, @"",nil];

It's because NSDictionary assumes the type of the other Keys? Is correct this manner of initialization?

+7  A: 

The prototype of the method you mentioned is

-(id)initWithObjectsAndKeys:(id)firstObject, ...;

Thus the first parameter must be an ObjC object. But the rest are passed by varargs. In C, any primitives can be passed as vararg arguments (think printf). Hence the compiler won't issue any warnings.

While the compiler is incapable of chekcing the types of the vararg arguments, it doesn't mean passing non-id into the method is valid.

KennyTM
I see... in fact when I have to get an Object by Key I have to do it with an objectForKey:(id)aKey then the Objects and then Keys must be (id).Thanks for the answer :-)
rubdottocom