我在使用这段代码时遇到了问题:
NSRect itemFrame;
id item;
// code to assign item goes here.
itemFrame.origin.y -= [item respondsToSelector:@selector(selectedHeight)] ? [item selectedHeight] : [self defaultSelectedHeight];
这是有问题的一点:
[item selectedHeight]
编译器假设返回类型是 id。我虽然添加 Actor 可以解决这个问题:
(float)[item selectedHeight]
但它不起作用。
我做错了什么? (我怀疑问题与解析与 id 相关的指针有关,但我找不到任何相关文档)。
最佳答案
你想要 [[item selectedHeight] floatValue]
,假设 selectedHeight 返回一个 NSNumber
。
关于objective-c - 如何将 id 转换为 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/307128/