将对象插入数组时崩溃

发布于 2024-11-16 07:39:56 字数 743 浏览 2 评论 0原文

在这段代码中,我从一个数组中选择一个字典,修改它并保存回另一个数组中。但我不知道为什么在这段代码的倒数第二行,即我插入字典的地方,它崩溃了(消息发送到已释放的实例) ).我该如何解决这个问题

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自演自醉 2024-11-23 07:39:56

当您将 array 分配给 tempArray 时,您不会仅仅因为转换它而使其可变。

它是一个 NSArray,因此您无法添加/删除其对象。

此外,还有一些不需要的初始化(tempArray 和 dictToBeChanged),因为您在初始化后立即用其他内容覆盖这些变量(从而造成泄漏)。

您需要的可能是这样的:

NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:plistPath];
NSMutableDictionary *dictToBeChanged = [[[array objectAtIndex:indexPath.row] mutableCopy] autorelease];
[dictToBeChanged setObject:[NSNumber numberWithBool:YES] forKey:@"isPaid"];
[array replaceObjectAtIndex:indexPath.row withObject:dictToBeChanged];

请注意,此代码不会对您的 plist 内容进行任何验证。

When you assign array to tempArray 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:

NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:plistPath];
NSMutableDictionary *dictToBeChanged = [[[array objectAtIndex:indexPath.row] mutableCopy] autorelease];
[dictToBeChanged setObject:[NSNumber numberWithBool:YES] forKey:@"isPaid"];
[array replaceObjectAtIndex:indexPath.row withObject:dictToBeChanged];

Note that this code doesn't do any validations on the contents of your plist.

云胡 2024-11-23 07:39:56

您可能希望将对象作为 temparray 添加到 tempArray 中,如下所示:

[tempArray addObjectsFromArray:array];

You may want to add the objects to tempArray as the temparray as follows:

[tempArray addObjectsFromArray:array];
单挑你×的.吻 2024-11-23 07:39:56

试试这个

  NSMutableArray *temp;
temp=[temp arrayByAddingObjectsFromArray:(NSArray *)otherArray];

try this

  NSMutableArray *temp;
temp=[temp arrayByAddingObjectsFromArray:(NSArray *)otherArray];
若能看破又如何 2024-11-23 07:39:56

您正在考虑内存管理问题。试试这个:

NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:plistPath];//Temp array is unecessary
NSMutableDictionary *dictToBeChanged; //No need to allocate a new instance

不直接相关,但是:

你的两个 alloc [init] 调用都是不必要的并且会导致泄漏。基本上,您正在做的是使用分配创建一个新的空白数组并将其分配给变量。然后,您立即将变量分配给另一个数组,丢失对刚刚创建的空白数组/字典的引用,这意味着它无法被释放。如果您稍后在代码中调用release,则会引起麻烦。

You are looking at a memory management issue. Try this:

NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:plistPath];//Temp array is unecessary
NSMutableDictionary *dictToBeChanged; //No need to allocate a new instance

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文