Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios app crash when calling a property with the underscore format

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.

like image 629
Nikita P Avatar asked Nov 26 '25 07:11

Nikita P


1 Answers

You should use self.var to retain sumthing when you are using @property so 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 when sumthing is released.

Basically a self.var = sumthing; is equivalent to assigning sumthing to var and then retaining it(var = [sumthing retain];). When you are directly doing it, it just does the assigning part and not the retaining. So when sumthing gets released, you will be pointing to a released variable when you use var and 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.

like image 63
iDev Avatar answered Nov 27 '25 19:11

iDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!