I have an NSInteger in my class like so
In @interface:
NSInteger currentRow;
@property (assign) NSInteger currentRow;
In @implementation:
@synthesize currentRow;
Doing [self setCurrentRow:0] seems to work fine, but using [self currentRow] just returns null for some reason, not sure why. When using breakpoints I can see that the value for currentRow is 0 so it has set fine, but I can’t get the value back.
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%pto see this value. You can display it in the debugger too, like this:print myObject. A pointer is displayed as a hexadecimal number, with a0xprefix.nilis 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. WithNSLog, you can use%@to print out an object’sdescription. In the debugger, like this:print-object myObject.Primitive types like
NSIntegeraren’t objects. Instead of storing a pointer, usually you just store the value. When you assign anNSIntegervariable, you make a copy of the value. You can see the value in the debugger usingprint. Or like this:NSLog("%ld", (long)currentRow). When you assign a primitive, you copy its value. Don’t use%@orprint-objectwith 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:
print-object currentRow, change it toprint currentRow.NSLog("%@", currentRow), change it toNSLog(%"ld", (long)currentRow).currentRowsomewhere else, where an object is required, change your instance variable and property types toNSNumber *, an object type. Set it with[self setCurrentRow:[NSNumber numberWithInt:0]].