为什么 (int)[@"-1" floatValue] 不返回 -1
我在 gdb 中测试了这个表达式:
(gdb) p (int)[@"-1" floatValue]
$2 = 1
但
(gdb) p (int)((float)[@"-1" floatValue])
$7 = -1
结果符合我的预期
为什么第一个表达式不返回 -1
?另外,[@"-1" floatValue]
的返回类型是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
gdb 不知道方法(或函数)的返回类型:
当您将表达式转换为
int
时,gdb 假定该方法返回int
。所以它知道它可以使用objc_msgSend
发送消息并处理从objc_msgSend
返回一个int
值。当您将表达式转换为
float
时,gdb 假定该方法返回一个float
。所以它知道应该使用objc_msgSend_fpret
发送消息并处理返回浮点数形式的值。这很重要,因为:
gdb doesn't know the return type of methods (or functions):
When you cast the expression to
int
, gdb assumes that the method returns anint
. So it knows it can useobjc_msgSend
to send the message and treat the return value fromobjc_msgSend
as anint
.When you cast the expression to a
float
, gdb assumes that the method returns afloat
. So it knows that it should useobjc_msgSend_fpret
to send the message and treat the return value as a float.This is important because: