I have declared a variable in .h:
@property (nonatomic, retain) NSString *var;
When i call a property variable using:
_var = sumthing;
the app crashes and when i call it as:
self.var = sumthing;
it works well.
Is there any difference in these two scenarios?
NOTE: I have not used @synthesize as it is not required to write this specifically.
You should use
self.varto retainsumthingwhen you are using@propertyso that it calls the setter method internally to retain it. If you are directly assigning it, the setter method wont be called and it will crash whensumthingis released.Basically a
self.var = sumthing;is equivalent to assigningsumthingtovarand then retaining it(var = [sumthing retain];). When you are directly doing it, it just does the assigning part and not the retaining. So whensumthinggets released, you will be pointing to a released variable when you usevarand your app crashes.If you still want to use it without using
self.var, you can try with_var = [sumthing retain];which could work.This has nothing to do with synthesize since you dont need to use it anymore. You can skip it.