简体   繁体   中英

Objc BAD_ACCESS when using a function to assign to a NSString property

Had this weird bug in my code and I was not able to solve it.

First I have this singelton class when I have a property called tempPassword.

 @private
NSString *_tempPassword;
 }

 @property (nonatomic, retain) NSString *tempPassword;

 +(User*)owner;
 -(void) setTempPassword:(NSString*)string;

The setTempPassword just takes this string and assigns it to tempPassword. The issue is that no matter what I did in the setTempPassword function, I always got BAD_ACCESS. Doest this have something to do with it being a predefined named and cant be used as I'm using it? Pulling my hair here...

If you aren't mucking around with the string further than just setting it and getting (pun intended) out of the way, then there is no need to override the setter for it. You mention that you already @synthesized the property, so overriding it is unnecessary anyways. Just call:

[[MySingleton owner]setTempPassword:@"Password"];

and all will be right as rain, provided you remove those overrides from your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM