gpt4 book ai didi

objective-c - ios中存档取消存档,如何处理错误?

转载 作者:行者123 更新时间:2023-11-29 04:20:44 27 4
gpt4 key购买 nike

我有一个业务对象,我需要更改其属性之一。我尝试获取该属性的字符串表示形式,然后修改它,就像我在下面的代码中尝试的那样

  NSMutableString *mutatingString = [ [ NSMutableString alloc ] init ] ;
mutatingString = ( NSMutableString * ) [ businessOject valueForKey:@"basicproperty" ] ;
[ mutatingString appendString:@"Modified" ];

当我热第三行时,我追加了字符串,我得到以下异常:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'Attempt to mutate immutable object with appendString:'

我的代码有什么问题吗?

最佳答案

您不应将 [businessOject valueForKey:@"basicproperty"] 的结果从 NSString* 转换为 NSMutableString*。

NSString* str  =   [businessOject valueForKey:@"basicproperty" ];
NSMutableString *mutatingString = [[NSMutableString alloc ] initWithString:str ] ;
[mutatingString appendString:@"Modified" ];
[businessOject setValue:mutatingString forKey:@"basicproperty" ]

关于objective-c - ios中存档取消存档,如何处理错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13014549/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com