Objective C:为什么我的 NSMutableArray 不起作用?

发布于 2024-10-03 08:40:46 字数 374 浏览 5 评论 0原文

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

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

发布评论

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

评论(3

献世佛 2024-10-10 08:40:46

如果您希望它保留下来,请保留您的 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 it addObject:g ?

Why not put them on separate lines and find out?

雪若未夕 2024-10-10 08:40:46

我知道你说对象分配正确,但我倾向于检查它。下面是经过更多调试的相同代码:

NSMutableArray *mColumns = [NSMutableArray array];
for( int i=0; i<5; i++ ) {
  MyColumn *col = [[MyColumn alloc] init];
  NSLog(@"%d: %@", i, col);
  [mColumns addObject:col];
  [col release];
}
NSLog(@"Array has %d elements, first is %@", [mColumns count], [mColumns objectAtIndex:0]);

问题可能是 (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:

NSMutableArray *mColumns = [NSMutableArray array];
for( int i=0; i<5; i++ ) {
  MyColumn *col = [[MyColumn alloc] init];
  NSLog(@"%d: %@", i, col);
  [mColumns addObject:col];
  [col release];
}
NSLog(@"Array has %d elements, first is %@", [mColumns count], [mColumns objectAtIndex:0]);

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.

你的他你的她 2024-10-10 08:40:46

嗯。我可以在您的代码中看到一个问题,但不会导致此问题:您正在将保留的对象添加到数组中;你需要先自动释放它们,这样就不会泄漏。

至于你的实际问题,我不知道。用空指针填充数组是不可能的;只有id类型的对象(不是任意指针)可以放入NSArray/NSMutableArray内部。

我知道您说过您已经检查了 -[MyColumn init],但最好检查一下它是否在这个位置生成了正确的对象。

columns = [NSMutableArray array];
for (int i = 0; i < 5; i++) {
  id c = [[[MyColumn alloc] init] autorelease];
  /* set a breakpoint here, and type `po c`
     into the debugger to see what was created
   */
  [columns addObject:c];
}

除非 -[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 an NSArray/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.

columns = [NSMutableArray array];
for (int i = 0; i < 5; i++) {
  id c = [[[MyColumn alloc] init] autorelease];
  /* set a breakpoint here, and type `po c`
     into the debugger to see what was created
   */
  [columns addObject:c];
}

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 to mColumns; is it retained by anything, for instance?

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