objective-c - 为什么 (int)[ @"-1"floatValue] 不返回 -1

标签 objective-c casting nsstring

我在 gdb 中测试了这个表达式:

(gdb) p (int)[@"-1" floatValue]
$2 = 1

但是

(gdb) p (int)((float)[@"-1" floatValue])
$7 = -1

如我所愿

为什么第一个表达式不返回-1?另外,[@"-1"floatValue] 的返回类型是什么?

最佳答案

gdb 不知道方法(或函数)的返回类型:

(gdb) p [@"-1" floatValue]
Unable to call function "objc_msgSend" at 0x155d08c: no return type information available.
To call this function anyway, you can cast the return type explicitly (e.g. 'print (float) fabs (3.0)')

当您将表达式转换为 int 时,gdb 假定该方法返回一个 int。所以它知道它可以使用 objc_msgSend发送消息并将 objc_msgSend 的返回值视为 int

当您将表达式转换为float 时,gdb 假定该方法返回float。所以它知道它应该使用 objc_msgSend_fpret发送消息并将返回值视为 float 。

这很重要,因为:

On the i386 platform, the ABI for functions returning a floating-point value is incompatible with that for functions returning an integral type. On the i386 platform, therefore, you must use objc_msgSend_fpret for functions that for functions [sic] returning non-integral type.

关于objective-c - 为什么 (int)[ @"-1"floatValue] 不返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7991218/

相关文章:

ios - 使用 AirDrop 在我的应用程序上接收 NSString

objective-c - @property设计问题的另一种适当使用

objective-c - OCUnit 中的简化断言

c# - 将 0.0 转换为 double 有什么问题?

ios - 从 xml 内容字符串访问关键字

ios - 点击手势到 UITextView 的一部分

ios - 使用 UIViewControllerInteractiveTransitioning 委托(delegate)协议(protocol)实现自定义 UIViewController 交互式过渡的正确方法

ios - 阻止应用程序使用 Facebook 单点登录?

c++ - 为什么将 int 转换为 bool 会发出警告?

c++ - 将 boost std::string::const_iterator 转换为 string 和 int c++