NSUInteger vs NSInteger, int vs unsigned, and similar cases

后端 未结 4 1851
被撕碎了的回忆
被撕碎了的回忆 2020-12-24 12:13

Anyone have the expertise to explain when to use NSUInteger and when to use NSInteger?

I had seen Cocoa methods returning NSInteger even in cases where the returned

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-24 12:30

    You should also be aware of integer conversion rules when dealing with NSUInteger vs. NSInteger:

    The following fragment for example returns 0 (false) although you'd expect it to print 1 (true):

    NSInteger si = -1;
    NSUInteger ui = 1;
    printf("%d\n", si < ui);
    

    The reason is that the [si] variable is being implicitly converted to an unsigned int!

    See CERT's Secure Coding site for an in-depth discussion around these 'issues' and how to solve them.

提交回复
热议问题