Ios app crash when calling property with underscore character

I declared a variable in .h:

@property (nonatomic, retain) NSString *var;

      

When I call the property variable using:

 _var = sumthing;

      

the app crashes and when I call it:

 self.var = sumthing;

      

it works well.

Is there a difference in these two scenarios?

NOTE. I have not used @synthesize

as it is not necessary to write this.

+3


source to share


2 answers


You have to use self.var

to save sumthing

when you use @property

to call the setter internally to save it. If you assign it directly, the setter method will not be called and it will fire when released sumthing

.

Basically, it's self.var = sumthing;

equivalent to assigning sumthing

to var

and then storing it ( var = [sumthing retain];

). When you do it directly, it just assigns the part, it doesn't save. So when sumthing

released you will point to the released variable when you use var

and your application will crash.



If you still want to use it without using it self.var

, you can try with help _var = [sumthing retain];

that might work.

This has nothing to do with fusion as you don't need to use it anymore. You can skip it.

+2


source


Have you @synthesized a property of your var with _var ..? If not, it will treat it as var. don't use _var.



0


source







All Articles