Objective C:为什么我的 NSMutableArray 不起作用?
我对 ObjC 及其可变数组还很陌生。这让我发疯;-)
我执行以下操作:
mColumns = [NSMutableArray array];
for( int i=0; i<5; i++ ) [mColumns addObject:[[MyColumn alloc] init]];
在代码之后,我的数组“mColumns”包含 5 个元素。但它们每个都是一个 NULL 指针! (或者至少调试器是这么告诉我的)。 我已经检查过代码是否
[[MyColumn alloc] init]
给了我一些有效的对象,所以我不知道为什么我的数组被 0x0 填充。 你能给我一个提示吗?
I'm quite new to ObjC and its mutable arrays. This is driving me crazy ;-)
I do the following:
mColumns = [NSMutableArray array];
for( int i=0; i<5; i++ ) [mColumns addObject:[[MyColumn alloc] init]];
After that code my array "mColumns" contains 5 elements. But each of them is a NULL-Pointer! (Or at least that's what the debugger tells me).
I already checked that the code
[[MyColumn alloc] init]
Gives me some valid objects, so I have no idea why my array gets filled with 0x0s.
Can you give me a hint?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望它保留下来,请保留您的 mutableArray。在当前事件循环结束时,它将自动释放,就像它在 autoreleasePool 中一样。
到那时,所有的赌注都会被取消。您的 mColumns 变量只是指向一块垃圾内存,可能是另一个对象,可能是半个对象,甚至您仍然可以获得正确的计数,甚至是一个包含的对象,但在某些时候您的应用程序会碰撞。
简单说一下,如果
[[mColumns objectAtIndex:x] addObject:g];
导致您的应用崩溃,是[mColumns objectAtIndex:x]
导致了崩溃还是它是addObject:g
吗?为什么不把它们放在不同的行上并找出答案呢?
Retain your mutableArray if you want it to stick around. At the end of the current event-loop it will be automatically deallocated, as it is in the autoreleasePool.
At that point all bets are off. Your mColumns variable just points to a junk piece of memory, maybe another object, maybe half an object, maybe even you can still get the correct count or even a contained object, but at some point your app will crash.
Just a quick point, if
[[mColumns objectAtIndex:x] addObject:g];
crashes your app is it[mColumns objectAtIndex:x]
that is causing the crash or is itaddObject:g
?Why not put them on separate lines and find out?
我知道你说对象分配正确,但我倾向于检查它。下面是经过更多调试的相同代码:
问题可能是 (a) 未正确创建对象,或者 (b) 数组确实具有元素,但您遇到了不同的问题。
I know you say that the objects are allocated correctly but I'd be inclined to check it. Here's the same code with more debugging:
The problem is likely to be either (a) the object is not being created correctly, or (b) the array does have the elements and you have a different problem.
嗯。我可以在您的代码中看到一个问题,但不会导致此问题:您正在将保留的对象添加到数组中;你需要先自动释放它们,这样就不会泄漏。
至于你的实际问题,我不知道。用空指针填充数组是不可能的;只有
id
类型的对象(不是任意指针)可以放入NSArray
/NSMutableArray
内部。我知道您说过您已经检查了
-[MyColumn init]
,但最好检查一下它是否在这个位置生成了正确的对象。除非
-[MyColumn init]
正在制作一些非常时髦的对象,否则我无法弄清楚问题是什么。 我想知道mColumns
是否发生了奇怪的事情;例如,它是否被任何东西保留?Hmmm. I can see one problem in your code, but not one that would cause this issue: you are adding retained objects to the array; you'll want to autorelease those first so you don't leak.
As for your actual problem, I don't know. It's impossible to fill an array with null pointers; only objects of type
id
(not arbitrary pointers) can be put inside of anNSArray
/NSMutableArray
.I know you said that you have checked
-[MyColumn init]
, but it would be a good idea to check that it is producing the proper objects in this very spot.Unless
-[MyColumn init]
is making some really funky objects, I can't figure out what the problem would be. I wonder whether something weird is happening tomColumns
; is it retained by anything, for instance?