I’m slightly confused with a certain concept. The best way to ask my question is to illustrate it with an example. Now in the below code I declare a property called loan and I synthesize this property. Now this might seem like a nooby question but I’m assigning the value “250.00” to loan. I’ve seen tutorials around the web that might do “self.loan = 250.00;” VS my “loan = 250.00;” Both ways seem to accomplish the same thing. But why do people use “self.propertyName” to access a property, when using the property name itself is enough?
//ClassA.h
@interface ClassA: UIViewController
@property double loan;
@end
//ClassA.m
@implementation ClassA
@synthesize loan;
-(void)doSomething{
loan = 250.00;
}
@synthesizealso generates an underlying instance variable for your property, which is unfortunately named the same as the property by default.This means that when you use
loan = 250.0, you’re really accessing the underlying instance variable directly, rather than via. the property accessor. You can see this if you change your@synthesizeline to:Now your code no longer compiles and you must use either
self.loanto access it via. the generated property accessors, or_loanto access the underlying instance variable.