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
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
相关文章推荐
- Java Object类方法
- Android中的JSONObject和JSONArray的使用
- xml 与dto的相互转换
- iOS Objective-C 中的 initialize 和 init 的介绍
- GitHub 上排名前 100 的 Objective-C 项目简介
- object有哪些公用方法
- PHP SPL中提供了SplFileInfo和SplFileObject两个类来处理文件操作
- Objective-c 中CGGeometry几何类常用方法简单整理
- HTML插入Flash的全兼容完美解决方案-SWFObject
- 字符串转换成JSONObject
- JSONObject获取key值报空指针
- dede Fatal error: Call to a member function read() on a non-object
- SQL:object_id的应用
- Java day07 复写Object 类的equals(Object)方法
- Object.create() 是什么?继承(继承特定的)
- 为GCD队列绑定NSObject类型上下文数据-利用__bridge_retained(transfer)转移内存管理权-备
- Objective-C和Java的简单对比
- TStack,TQueue,TObjectList,TObjectStack等等
- Jackson 忽略不需要的字段
- Objective-c UIViewController原生态跳转动画