In Objective-C, it is important to distinguish between objects and primitive types.
An object always saved as a pointer, which is the location of the object in memory. A pointer is just a number. With NSLog you can use %p to see this value. You can also display it in the debugger, for example: print myObject . The pointer is displayed as a hexadecimal number with the prefix 0x . nil is essentially zero zero ( 0x0000 ). When you select an object, you will get a pointer that is not non-zero. When you assign an object to a variable, you simply copy the memory address, not duplicate the object. With NSLog you can use %@ to print the description object. In the debugger, for example: print-object myObject .
Primitive types , such as NSInteger , are not objects. Instead of storing a pointer, you usually just keep 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. Do not use %@ or print-object with primitives - they expect objects.
(I say βusually you just keep the valueβ because you can also create pointers to primitive types. In situations like yours, however, this is not necessary.)
[self currentRow] returns 0, just like you set it. (Moreover, since Objective-C guarantees the initialization of instance variables, it will return 0, even if you do not set it.)
The problem is that you are expecting a pointer to an object. How you fix your code depends on how you use it:
- If you are using
print-object currentRow , change it to print currentRow . - If you are using
NSLog("%@", currentRow) , change it to NSLog(%"ld", (long)currentRow) . - If you use
currentRow somewhere else where an object is required, change your instance variable and property types to NSNumber * , the object type. Install it using [self setCurrentRow:[NSNumber numberWithInt:0]] .
paulmelnikow
source share