NSMutablearray 的最后一个元素被损坏并从自定义对象变成标准 NSObject
今天我遇到了一个关于 NSMutableArray 的奇怪问题。 我正在解析一个 XML 文件,并将解析的项目添加为自定义对象。共有 37 项。 因此,当我的视图加载时,我这样做了,作为测试:
[parser loadDataBase];
ProductItem* item = [parser.productDetail.prodItems objectAtIndex:36];
NSLog(@"test 1 %@", item.idItem);
self.product = parser.productDetail;
item = [self.product.prodItems objectAtIndex:36];
NSLog(@"test 2 %@", item.idItem);
[parser release];
此时,一切正常。两个 NSLog 都打印可变数组中最后一项的正确值。
问题是当我尝试将这些项目添加到表中时。 当应用程序尝试获取索引 36 处的项目以显示其属性时,它获取的不是 ProductItem 自定义对象,而是 NSObject 对象...最后一个项目的所有内容都将丢失,被纯粹的 NSObject 替换。
除了解析器之外,我对该类或任何其他类中的数组绝对不执行任何操作。 当它离开解析器时一切都很好,当我从解析器读取它时一切都很好,当我检查是否从解析器正确获取所有值时一切都很好。但不知何故,最后一个值在此之后被损坏,即使我没有做任何可能导致这种情况的事情。
这是我在 cellForRow 中使用的代码:
NSLog(@"index %i", indexPath.row);
ProductItem* item = [self.product.prodItems objectAtIndex:indexPath.row];
cell2.itemName.text = item.name;
行是 36,最后一行,然后繁荣!当我尝试读取 name 属性时 EXC_BAD_ACCESS 。
这里有人知道可能发生什么吗?我以前从未遇到过这样的问题 感谢您的时间和关注!
I run into a weird problem with a NSMutableArray today.
I'm parsing an XML file and I add the parsed items as custom objects. There are 37 items in total.
So, when my view loads, I did this, as a test:
[parser loadDataBase];
ProductItem* item = [parser.productDetail.prodItems objectAtIndex:36];
NSLog(@"test 1 %@", item.idItem);
self.product = parser.productDetail;
item = [self.product.prodItems objectAtIndex:36];
NSLog(@"test 2 %@", item.idItem);
[parser release];
At this point, everything works just fine. Both NSLog print the correct value for the last item in the mutable array.
The problem is when I try to add these items into a table.
When the app tries to get the item at index 36, to display its properties, instead of a ProductItem custom object, it gets a NSObject object... everything is lost for the last item, being replaced with a mere NSObject.
I do absolutely nothing with the array in that class, or any other class, except the parser.
Everything is ok when it leaves the parser, everything is ok when I read it from the parser, everything is ok when I check to see if I got all the values correctly from the parser. But somehow, the last value gets corrupted after this, even though I don't do anything that might cause this.
Here's the code I use in the cellForRow:
NSLog(@"index %i", indexPath.row);
ProductItem* item = [self.product.prodItems objectAtIndex:indexPath.row];
cell2.itemName.text = item.name;
The row is 36, the last one, and boom! EXC_BAD_ACCESS when I try to read the name property.
Does anyone here have a clue about what might be happening? I never ran into such a problem before
Thank you for your time and attention!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在同事的帮助下,我了解到发生了什么事。
我在解析器的 dealloc 中释放了 currentParsedItem,因此在 [解析器发布] 之前一切看起来都正常;但在那之后,我的可变数组中的最后一个对象“currentParsedItem”在解析器的 dealloc 函数中被设置为 nil,我最终得到了一个空白的 NSObject
希望这个技巧可以帮助其他人解决类似的问题!
With some help from a colleague, I found out what was happening.
I was releasing my currentParsedItem in the parser's dealloc, so everything looked ok before the [parser release]; but after that the, "currentParsedItem" that was the last object in my mutable array was being set to nil in the dealloc function of the parser, and I ended up with a blank NSObject
Hope this tip helps others with a similar problem!