简体   繁体   English

使用函数分配给NSString属性时出现对象BAD_ACCESS

[英]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. 首先,当我有一个名为tempPassword的属性时,便拥有这个singelton类。

 @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. setTempPassword只是获取此字符串并将其分配给tempPassword。 The issue is that no matter what I did in the setTempPassword function, I always got BAD_ACCESS. 问题是,无论我在setTempPassword函数中做了什么,我总是得到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. 您提到您已经@synthesized属性,因此无论如何都不必覆盖它。 Just call: 只需致电:

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

and all will be right as rain, provided you remove those overrides from your code. 只要您从代码中删除这些覆盖,一切都会如雨后春笋般正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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