最近我的代码中出现了一些意外的编译错误,我感到非常困惑。我做了一些故障排除,我想出了这个位的测试代码:performSelector的结果:与标准点符号调用不同?
1 float performSelectorResult, messageResult;
2 SEL selector = @selector(smallContentCellMargin);
3
4 NSLog (@"selector %s result %f", selector, [defaults performSelector:selector]);
5 NSLog (@"message result is %f", defaults.smallContentCellMargin);
6
7 performSelectorResult = [defaults performSelector:selector];
8 messageResult = defaults.smallContentCellMargin;
如果我运行的代码,是,我得到第7行编译错误:“不兼容的类型分配”。但是,如果我注释掉第7行代码的构建和运行,在NSLog的结果是:
2009-07-21 18:31:44.823 ICE[24347:20b] selector smallContentCellMargin result 0.000000
2009-07-21 18:31:44.830 ICE[24347:20b] message result is 7.000000
我已经使用非常相似的代码来检索一个UIFont,但从来没有一个浮点数。有什么我不知道的,或者这是Objective-C中的错误吗?我很困惑。
我尝试了-valueForKey的快速测试:'float quickTest = [defaults valueForKey:@“smallContentCellMargin”];'当我尝试编译代码时,出现“初始化中的不兼容类型”错误。我在文档中查找了-valueForKey,并且它说它返回类型(id) - 我做错了什么,或者在这种情况下是否与-performSelector相同? – JoBu1324 2009-07-22 03:04:13