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 · Nov 30, 2015 · 3953 views
    This topic created in 3844 days ago, the information mentioned may be changed or developed.
    这是代码







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


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

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

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

    何来溢出?
    surfire91
        9
    surfire91  
       Nov 30, 2015
    @wizardforcel 这还不是溢出?
    wezzard
        10
    wezzard  
       Nov 30, 2015
    lou zhu zai diao yu.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4316 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 04:11 · PVG 12:11 · LAX 21:11 · JFK 00:11
    ♥ Do have faith in what you're doing.