Exactly. Your assumption is correct. You can implement property yourself and add logging to check your assumption one more time
In your @interface section:
@property(nonatomic) NSInteger foo;
// nonatomic keyword is not really required but
// it is better to add it since we will implement
// property as nonatomic
In @implementation section:
- (void)setFoo:(NSInteger)foo
{
_foo = foo; // _foo variable is implicitly generated by compiler
NSLog(@"set foo %d", _foo);
}
- (NSInteger)foo
{
NSLog(@"get foo %d", _foo);
return _foo;
}
Then run
self.foo = 0;
self.foo += 1;
you should receive in debug window:
set foo 0
get foo 0
set foo 1
UPDATE:
- Re: "at the assembly level, is there any difference between
self.foo += 1;
andself.foo = self.foo + 1;
?"
No. For both [self setFoo:([self foo] + 1)];
will be called.
- Re: What if we're not talking about a property, and bar is just a regular int, at the assembly level is there a difference between
bar += 1;
andbar = bar + 1;
?
Yes. But only if compile time optimization is turned off.
bar += 1;
is faster. It will be compiled to something like:
mov eax,dword ptr [bar]
inc eax // difference is here!
mov dword ptr [bar],eax
And bar = bar + 1;
to:
mov eax,dword ptr [bar]
add eax,1 // difference is here!
mov dword ptr [bar],eax