V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
finab
V2EX  ›  iDev

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

  •  
  •   finab ·
    Finb · 2015-11-30 11:35:17 +08:00 · 3374 次点击
    这是一个创建于 3063 天前的主题,其中的信息可能已经有所发展或是发生改变。
    这是代码







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


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

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

    NSLog(@"%u", -1); // 输出 4294967295
    finab
        5
    finab  
    OP
       2015-11-30 12:01:47 +08:00
    @loveuqian 很早前的个人的习惯 ,现在已经不这样写了,就像 私有类一样 定义在内部的类 - -~
    fogisland
        6
    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
        7
    ljbha007  
       2015-11-30 16:05:04 +08:00
    @finab 因为 int -> uint 不会溢出 uint -> int 会溢出
    wizardforcel
        8
    wizardforcel  
       2015-11-30 16:12:22 +08:00 via Android
    @ljbha007 只是保持底层字节不变 转换表达方式而已

    何来溢出?
    surfire91
        9
    surfire91  
       2015-11-30 18:28:21 +08:00
    @wizardforcel 这还不是溢出?
    wezzard
        10
    wezzard  
       2015-11-30 20:00:44 +08:00
    lou zhu zai diao yu.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   945 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:04 · PVG 04:04 · LAX 13:04 · JFK 16:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.