我在 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/