gpt4 book ai didi

objective-c - 当我尝试替换可变数组项时出现异常

转载 作者:行者123 更新时间:2023-11-29 04:47:59 24 4
gpt4 key购买 nike

我遇到了这个异常:

[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance

当我尝试用另一个元素替换特定元素时:

编辑:这是我的整个代码:

//declaring an AppDelegate instance
AppDelegate *myAppDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

//get the array in which we have stored all the choosed themes
NSMutableArray *aMutableArray=myAppDelegate.themesChoosed;


for (int i=0; i<[aMutableArray count]; i++) {
NSString *str=[NSString stringWithString:[aMutableArray objectAtIndex:i]];
if ([str isEqualToString:@"B1"]) {
[aMutableArray replaceObjectAtIndex:i withObject:@"B2"];
}
}

我确保数组中确实存在 B1 元素。

最佳答案

在进入 for 循环之前,你的 NSMuatbleArray 发生了什么?

它是属性(property)吗?如果是的话,属性(property)申报是什么?您使用了复制吗?

如果您实现这样的属性:

@property (nonatomic, copy) NSMutableArray *myArray;

...那么您可能会遇到这样的问题,因为合成的 setter 将 copy 发送到数组,这会导致不可变的副本。如果是这种情况,您需要实现自己的 setter,在数组上调用 mutableCopy (或者只使用 retain 来代替,并以稍微不同的方式设计您的代码)。

编辑:

根据您下面的评论和更新的代码,我确信问题一定与应用程序委托(delegate)上的数组不可变有关。

试试这个:

NSMutableArray *mutableThemeseChoosed = [NSMutableArray arrayWithArray:myAppDelegate.themesChoosed];

关于objective-c - 当我尝试替换可变数组项时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9329205/

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