The root of the problem is that int
and NSInteger
can be different sizes. So, you're assigning a pointer to an NSInteger
to a property of type NSInteger*
, which is okay (but unusual). However, you're dereferencing that value as an int
, which happens not to be the same size.
However, I think that you may also be confused about how you need to declare a property to hold an NSInteger
. NSInteger
is just a simple integer type, not an object, so there's no need to use a pointer to refer to it. This is doubly true since you seem to be declaring the NSInteger
whose address you're taking on the stack, so the pointer will be invalid as soon as the method where you do the assignment ends.
You'll be much better off using a plain old NSInteger
property and not trying to use a pointer here. Do this instead:
@property (assign) NSInteger freeTrialCounter;
//...
NSInteger a = 2;
self.freeTrialCounter = a;
NSLog(@"Free Trial Counter: %ld", self.freeTrialCounter);
NSInteger b = self.freeTrialCounter;
NSLog(@"B: %ld", b);
Note that I've also changed the type of b
to NSInteger
to match the property type. Since they're not necessarily the same size, mixing the two can (as you've seen) cause problems. Likewise, I've changed the format specifier from %d
to %ld
to match the 64-bit size of NSInteger
.