您的位置:首页 > 移动开发 > Objective-C

Attempt to mutate immutable object with appendString

2016-03-21 16:45 951 查看
@property (nonatomic,copy)
NSMutableString * addressName;

self.addressName = [NSMutableString
string];
for (NSString *name
in self.headerTitleArray) {
[self.addressName
appendString:name];
}
自己很纳闷为什么明明创建的是NSMutableString,还会报错,上网查了资料
The problem is due to the copy attribute on your property. When you assign a value to the property, it creates a copy. But the copy is made with copy and not mutableCopy so you actually end up assigning an NSString to the property and not an NSMutableString.

Get rid of copy on the property or implement your own custom "setter" method that calls mutableCopy.

结果我就重写了
set方法

- (void)setAddressName:(NSMutableString *)addressName
{
_addressName = [addressName
mutableCopy];
}
因为属性没有mutableCopy,所以实际还是copy
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: