rub*_*com 2 c c++ cocoa objective-c nsdictionary
对不起,如果问题不正确,我是Objective-C的新手.
我理解为什么这段代码会抛出警告:"警告:传递'initWithObjectsAndKeys:'的参数1'使得整数指针没有"
NSDictionary *dictNames =
[[NSDictionary alloc] initWithObjectsAndKeys:
3, @"",
4, @"",
5, @"",nil];
Run Code Online (Sandbox Code Playgroud)
NSDictionary的键和值必须是NSObject而不是基本类型,如整数3,4和5.(如果需要,请纠正我).
但是我不明白为什么这个警告会消除第一个键的唯一"正确输入".
NSDictionary *dictNames =
[[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInteger:3], @"",
4, @"",
5, @"",nil];
Run Code Online (Sandbox Code Playgroud)
这是因为NSDictionary假定其他键的类型?这种初始化方式是否正确?
你提到的方法的原型是
-(id)initWithObjectsAndKeys:(id)firstObject, ...;
Run Code Online (Sandbox Code Playgroud)
因此,第一个参数必须是ObjC对象.但其余的都是由varargs传递的.在C中,任何基元都可以作为vararg参数传递(想想printf
).因此编译器不会发出任何警告.
虽然编译器无法取消vararg参数的类型,但这并不意味着将非传递id
给方法是有效的.