iOS 中负数会大于正数吗? 为什么 -3 > 4(某个变量的值) 是 true 呢?

2015-11-30 11:35:17 +08:00
 finab
这是代码







而这样写就是对的, count 返回的不就是数值类型么?


这里有啥知识点?求大牛解惑啊 - -
3380 次点击
所在节点    iDev
10 条回复
finab
2015-11-30 11:43:51 +08:00
count 方法返回的是 NSUInteger ,我的 index 是 NSInteger
这两种数据类型比较会存在问题吧

fogisland
2015-11-30 11:52:54 +08:00
@finab 这是 C 语言的范畴了, int 会被强转为 uint ,因此 -1 > _group.count ;
直接写 -1 > 2 返回 false, 是因为常数的默认类型都是 int , 不会发生类型转换
loveuqian
2015-11-30 11:55:33 +08:00
为啥你自定义类 view 是小写字母开头
finab
2015-11-30 11:57:12 +08:00
@fogisland 我比较好奇的是
int 与 uint 比较,会把 int 转成 uint, 而不是将 uint 转成 int - -

NSLog(@"%u", -1); // 输出 4294967295
finab
2015-11-30 12:01:47 +08:00
@loveuqian 很早前的个人的习惯 ,现在已经不这样写了,就像 私有类一样 定义在内部的类 - -~
fogisland
2015-11-30 12:59:29 +08:00
@finab
给你个链接看看:

https://www.securecoding.cert.org/confluence/display/cplusplus/INT02-CPP.+Understand+integer+conversion+rules

"If the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type is converted to the type of the operand with unsigned integer type."
ljbha007
2015-11-30 16:05:04 +08:00
@finab 因为 int -> uint 不会溢出 uint -> int 会溢出
wizardforcel
2015-11-30 16:12:22 +08:00
@ljbha007 只是保持底层字节不变 转换表达方式而已

何来溢出?
surfire91
2015-11-30 18:28:21 +08:00
@wizardforcel 这还不是溢出?
wezzard
2015-11-30 20:00:44 +08:00
lou zhu zai diao yu.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/239962

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX