我有一个业务对象,我需要更改其中一个属性。我尝试获取该属性的字符串表示然后修改它,就像我在尝试下面的代码
NSMutableString *mutatingString = [ [ NSMutableString alloc ] init ] ;
mutatingString = ( NSMutableString * ) [ businessOject valueForKey:@"basicproperty" ] ;
[ mutatingString appendString:@"Modified" ];
当我在appendString的第三行加热时,我得到以下异常:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'Attempt to mutate immutable object with appendString:'
我的代码出了什么问题?
答案 0 :(得分:1)
您不应将[businessOject valueForKey:@“basicproperty”]的结果从NSString *转换为NSMutableString *。
NSString* str = [businessOject valueForKey:@"basicproperty" ];
NSMutableString *mutatingString = [[NSMutableString alloc ] initWithString:str ] ;
[mutatingString appendString:@"Modified" ];
[businessOject setValue:mutatingString forKey:@"basicproperty" ]