我对这个代码有问题:

NSRect itemFrame;
id item;
// code to assign item goes here.
itemFrame.origin.y -= [item respondsToSelector:@selector(selectedHeight)] ? [item selectedHeight]   : [self defaultSelectedHeight];

这就是问题所在:
[item selectedHeight]

编译器假定返回类型为id.i,但是添加强制转换可以解决以下问题:
(float)[item selectedHeight]

但没用。
我做错什么了?(我怀疑问题是解决与ID相关的指针,但我找不到任何相关文档)。

最佳答案

如果选择的高度返回[[item selectedHeight] floatValue],则需要NSNumber

关于objective-c - 如何将ID强制转换为浮点数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/307128/

10-11 22:29
查看更多