NSInteger set to 0 but returns nil

后端 未结 3 1163
野的像风
野的像风 2020-12-17 18:51

I have an NSInteger in my class like so

In @interface:

NSInteger currentRow;    

@property (assign) NSInteger currentRow;

In @

相关标签:
3条回答
  • 2020-12-17 18:58

    In Objective-C, it's important that you distinguish between objects and primitive types.

    An object is always stored as a pointer, which is the object's location in memory. A pointer is just a number. With NSLog, you can use %p to see this value. You can display it in the debugger too, like this: print myObject. A pointer is displayed as a hexadecimal number, with a 0x prefix. nil is essentially location zero (0x0000). When you allocate any kind of object, you'll get a pointer which isn't zero. When you assign an object to a variable, you are simply copying the memory address, not duplicating the object. With NSLog, you can use %@ to print out an object's description. In the debugger, like this: print-object myObject.

    Primitive types like NSInteger aren't objects. Instead of storing a pointer, usually you just store the value. When you assign an NSInteger variable, you make a copy of the value. You can see the value in the debugger using print. Or like this: NSLog("%ld", (long)currentRow). When you assign a primitive, you copy its value. Don't use %@ or print-object with primitives — they expect objects.

    (I say "usually you just store the value," because you can make pointers to primitive types, too. In situations like yours however it's not necessary.)

    [self currentRow] returns 0, just like you set it. (Furthermore, because Objective-C guarantees initialization of instance variables, it'll return 0 even if you don't set it.)

    The problem is that you're expecting a pointer to an object. How you fix your code depends on how you're using it:

    • If you're using print-object currentRow, change it to print currentRow.
    • If you're using NSLog("%@", currentRow), change it to NSLog(%"ld", (long)currentRow).
    • If you're using currentRow somewhere else, where an object is required, change your instance variable and property types to NSNumber *, an object type. Set it with [self setCurrentRow:[NSNumber numberWithInt:0]].
    0 讨论(0)
  • 2020-12-17 18:59

    The getter method will be synthesized as - (NSInteger)currentRow so it should work just fine. But how do you check if it works? With NSLog(@"%@", ...)? Than you should use %d.

    If you want it to be an object you should use NSNumber and a retain property.

    0 讨论(0)
  • 2020-12-17 19:12

    NSInteger is not object, it's typedef'd to int or something like that. Thus, if you set currentRow to 0 and then try to get it back, null (aka 0) is totally correct value.

    0 讨论(0)
提交回复
热议问题