将对象插入数组时崩溃
在这段代码中,我从一个数组中选择一个字典,修改它并保存回另一个数组中。但我不知道为什么在这段代码的倒数第二行,即我插入字典的地方,它崩溃了(消息发送到已释放的实例) ).我该如何解决这个问题
NSArray *array=[NSArray arrayWithContentsOfFile:plistPath];
NSLog(@"array before %@",array);
NSMutableArray *tempArray=[[NSMutableArray alloc]init];
tempArray=(NSMutableArray*)array;
NSMutableDictionary *dictToBeChanged=[[NSMutableDictionary alloc]init];
dictToBeChanged=[tempArray objectAtIndex:indexPath.row];
[dictToBeChanged setObject:[NSNumber numberWithBool:YES] forKey:@"isPaid"];
[tempArray removeObjectAtIndex:indexPath.row];
[tempArray insertObject:dictToBeChanged atIndex:indexPath.row];
NSLog(@"array after %@",tempArray);
In this code from an array i am selecting a dictionary,modifying it and saving back in another array .but i dont know why at the second last line of this code ie where i am inserting the dict it is crashing (message sent to deallocated instance).how can i fix this
NSArray *array=[NSArray arrayWithContentsOfFile:plistPath];
NSLog(@"array before %@",array);
NSMutableArray *tempArray=[[NSMutableArray alloc]init];
tempArray=(NSMutableArray*)array;
NSMutableDictionary *dictToBeChanged=[[NSMutableDictionary alloc]init];
dictToBeChanged=[tempArray objectAtIndex:indexPath.row];
[dictToBeChanged setObject:[NSNumber numberWithBool:YES] forKey:@"isPaid"];
[tempArray removeObjectAtIndex:indexPath.row];
[tempArray insertObject:dictToBeChanged atIndex:indexPath.row];
NSLog(@"array after %@",tempArray);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您将
array
分配给tempArray
时,您不会仅仅因为转换它而使其可变。它是一个 NSArray,因此您无法添加/删除其对象。
此外,还有一些不需要的初始化(tempArray 和 dictToBeChanged),因为您在初始化后立即用其他内容覆盖这些变量(从而造成泄漏)。
您需要的可能是这样的:
请注意,此代码不会对您的 plist 内容进行任何验证。
When you assign
array
totempArray
you don't make it mutable just because you cast it.It's an
NSArray
, so you can't add/remove its objects.Also, there are a few unneeded initializations (of tempArray and dictToBeChanged) since you're overwriting those variables with something else right after initializing (thus creating leaks).
What you need is probably something like this:
Note that this code doesn't do any validations on the contents of your plist.
您可能希望将对象作为
temparray
添加到 tempArray 中,如下所示:You may want to add the objects to tempArray as the
temparray
as follows:试试这个
try this
您正在考虑内存管理问题。试试这个:
不直接相关,但是:
你的两个 alloc [init] 调用都是不必要的并且会导致泄漏。基本上,您正在做的是使用分配创建一个新的空白数组并将其分配给变量。然后,您立即将变量分配给另一个数组,丢失对刚刚创建的空白数组/字典的引用,这意味着它无法被释放。如果您稍后在代码中调用release,则会引起麻烦。
You are looking at a memory management issue. Try this:
Not directly related but:
Both of your alloc [init] calls are unnecessary and causing leaks. Basically what you are doing is creating a new blank array with the allocation and and assigning it to a variable. Then you immediately assign your variable to another array, losing the reference to the blank array/dictionary you just created, which means it can't get released. If you are calling release later in your code it will cause trouble.