objective-c - 如何将 id 转换为 float ?

标签 objective-c macos casting

我在使用这段代码时遇到了问题:

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/

相关文章:

ios - 如何创建一个 "array of selectors"

ios - 如何在 iOS 的自动布局中设置 View 的动态宽度和高度?

swift - 将 PFObject 指针转换为解析中的子类(swift)

iOS - 访问未知 View Controller 中的属性

iphone - 在应用程序中使用 iPhone 默认警报声音

objective-c - 自动引用计数 : Pointer to non-const type 'NSError *' with no explicit ownership

iphone - UITableView 处于编辑模式 - 按删除键使我的应用程序崩溃

ios - 使用 AttributedString 立即格式化 UITextView

macos - 如何在 mac 中通过 cli 设置默认钥匙串(keychain)?

c++ - 转换 Derived** → Base** 是错误的吗?还有什么选择?