开发者

Casting warnings in methods with variable arguments

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 "co开发者_Python百科rrect 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜