int vs NSNumber vs NSInteger

后端 未结 4 1370
迷失自我
迷失自我 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:32

    1 is a literal and the compiler will make it the appropriate numeric type if that's appropriate.

    Note, however, that while NSInteger and int are to most intents and purposes the same thing, NSNumber is an object type. For that case, your code doesn't make sense (and shouldn't compile without warnings).

    (Actually, there are some circumstances where it would make a kind of sense, in terms of pointer arithmetic, but that's totally not what you want.)

    For the NSNumber case, you'd instead want something like:

    day = [NSNumber numberWithInt:[day intValue] + 1];
    

提交回复
热议问题