NSUInteger vs NSInteger, int vs unsigned, and similar cases

后端 未结 4 1825
被撕碎了的回忆
被撕碎了的回忆 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:41

    By default, an integer is assumed to be signed. In other words the compiler assumes that an integer variable will be called upon to store either a negative or positive number. This limits the extent that the range can reach in either direction. For example, a 32-bit int has a range of 4,294,967,295. In practice, because the value could be positive or negative the range is actually −2,147,483,648 to +2,147,483,647. If we know that a variable will never be called upon to store a negative value, we can declare it as unsigned, thereby extending the (positive) range to 0 to +4,294,967,295. So i would say, its okay to use NSInteger when you know that you have a restricted output range. I personally use NSUInteger if I needed to return really big positive only numbers

提交回复
热议问题