int vs NSNumber vs NSInteger

后端 未结 4 1366
迷失自我
迷失自我 2021-02-02 10:09

I have a line of code that will work differently depending on the datatypes \"day\" and \"1\". I believe it is the following although I will check my source code later.

4条回答
  •  再見小時候
    2021-02-02 10:48

    NSInteger is a type definition that describes an integer - but it is NOT equivalent to int on 64-bit platforms.
    You can examine the typedef by cmd-clicking on NSInteger in Xcode.
    NSInteger is defined as int when building a 32-bit app and as long for 64-bit apps.
    Most of the time you can replace int with NSInteger, but there are some things to consider when doing so.
    Apple's 64-Bit Transition Guide for Cocoa has some information about that.

    NSNumber is a class that helps you to store numeric types as object. It has methods to convert between different types and methods to retrieve a string representation of your numeric value.
    If you use a variable day of type NSNumber* the way you did in your example, you are not modifying the value of day but its memory address.

    If you are working with time values you also could take a look at NSDate.

提交回复
热议问题