在Objective-C中分析代码时出现奇怪的消息

时间:2012-11-16 08:20:10

标签: objective-c nsarray message code-analysis

我有一个数组,我用复选框,滑块和NSTextField填充值。 见下文001.显然它很有效。

然而,在分析我的代码时,我收到了以下消息:

/Users/ronny/DEV/0200-ObjC4/Egg&Breakfast/Classes/TimeController.m:358:24: Argument to 'NSArray' method 'arrayWithObjects:' should be an Objective-C pointer type, not 'NSInteger'

我尝试了几种类似的东西,比如使用intValue将这三行输出到(NSInteger)。没有成功。有什么想法错了吗?

NSArray *myValues = [NSArray arrayWithObjects:
                    [isAppointment state],     //Checkbox
                    [boxForEver    state],     //Checkbox
                    [boxMakeSound  state],     //Checkbox
                    [tickTackFlag  state],     //Checkbox
                    [txtRemark   stringValue], //NSTextField
                    [slideHour      intValue], //Slider
                    [slideMin       intValue], //Slider
                    [slideSec       intValue], //Slider
                    [startAuto     state],     //Checkbox
                    nil];

2 个答案:

答案 0 :(得分:3)

您无法将intfloatNSInteger等标量类型插入NSArray

必须插入指向NSObject或子类的指针,如错误消息中所述。

防止这种缺点的常用方法是使用NSNumber

例如:

NSInteger foo = 42;
[_myArray addObject:[NSNumber numberWithInteger:foo]];

在最近的Xcode版本中,您可以使用一些语法糖

[_myArray addObject:@42];

答案 1 :(得分:2)

调用stateintValue的所有行都不正确。

NSArray只能保存对Objective-C对象的引用。但stateintValue返回NSInteger,它是long的typedef(别名),它是C基元,而不是Objective-C对象引用。

您需要将整数包装在NSNumber个对象中。如果您使用的是Xcode 4.4或更高版本,则可以使用新的@(...)包装器语法将整数包装在NSNumber个对象中。您还可以使用新的数组文字语法@[...]来构建数组。

NSArray *myValues = @[
                    @([isAppointment state]),     //Checkbox
                    @([boxForEver    state]),     //Checkbox
                    @([boxMakeSound  state]),     //Checkbox
                    @([tickTackFlag  state]),     //Checkbox
                    [txtRemark   stringValue], //NSTextField
                    @([slideHour      intValue]), //Slider
                    @([slideMin       intValue]), //Slider
                    @([slideSec       intValue]), //Slider
                    @([startAuto     state]),     //Checkbox
                    ];