objective-c NSMutableArray 计数总是返回零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/633699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSMutableArray count always returns zero
提问by John Biesnecker
I'm sure I'm doing something silly, but this is driving me crazy.
我敢肯定我在做一些愚蠢的事情,但这让我发疯。
I'm trying to loop through database results, create objects from those results, and add the objects to an NSMutableArray. I've verified via NSLog calls that the data is being correctly read from the database and copied to the object, but the count for the NSMutableArray alwaysreturns 0.
我正在尝试遍历数据库结果,从这些结果创建对象,并将对象添加到 NSMutableArray。我已经通过 NSLog 调用验证了数据正在从数据库中正确读取并复制到对象中,但 NSMutableArray 的计数始终返回 0。
Here's the essence of the code:
下面是代码的本质:
while ([rs next]) {
Kana *htemp = [Kana alloc];
htemp.content = [rs stringForColumn:@"hiragana"];
[hiragana addObject:htemp];
}
NSLog(@"Hiragana contains %d objects", [hiragana count]);
Kana is derived from NSObject, and hiragana is an instance of NSMutableArray.
Kana 派生自 NSObject,平假名是 NSMutableArray 的一个实例。
I'm sure this is a rookie mistake, and I hope someone can set me straight. TIA! :)
我敢肯定这是一个新手错误,我希望有人能纠正我。蒂亚!:)
回答by Matt Ball
My guess, judging from the code you posted, is that you probably aren't allocating your array properly. When creating objects, you need to initialize them as well. Therefore, this:
从您发布的代码来看,我的猜测是您可能没有正确分配数组。创建对象时,还需要初始化它们。因此,这:
Kana *htemp = [Kana alloc];
Should be:
应该:
Kata *temp = [[Kana alloc] init];
All objects need to be initialized this way.Thus, if I'm correct and you haven't initialized your array, then your creation needs to go from this:
所有对象都需要以这种方式初始化。因此,如果我是正确的并且您还没有初始化您的数组,那么您的创建需要从这里开始:
NSMutableArray *hiragana = [NSMutableArray alloc];
to this:
对此:
NSMutableArray *hiragana = [[NSMutableArray alloc] init];
For optimization reasons, you should probably also specify an initial capacity as well if you have any idea how many objects you might hold:
出于优化原因,如果您知道可以容纳多少对象,您可能还应该指定初始容量:
[[NSMutableArray alloc] initWithCapacity:someNumber];
回答by Peter Hosey
Another common cause (not in your case, as it turns out, but generally) is forgetting to even allocate the array. If you haven't created an array yet, you're sending that countmessage to nil, so the result will always be 0.
另一个常见原因(不是在您的情况下,事实证明,但通常)是忘记甚至分配数组。如果您还没有创建数组,那么您正在将该count消息发送到nil,因此结果将始终为 0。
回答by Joel Levin
A few things:
一些东西:
- What happens if you put an NSLog call inside of the while loop? Verify that loop iterations are actually happening before blaming it on the array.
- Where are you creating the array hiragana? If you are doing it incorrectly for some reason and the array is nil, it might cause problems like this.
- If you do not have garbage collection on, be sure to do [htemp release] after adding it to the loop. addObject retains and each added item will leak from the loop. Again, this is only relevant if garbage collection is off.
- 如果将 NSLog 调用放在 while 循环中会发生什么?在将其归咎于数组之前,请验证循环迭代是否实际发生。
- 你在哪里创建数组平假名?如果由于某种原因你做错了并且数组为零,它可能会导致这样的问题。
- 如果您没有开启垃圾回收,请务必在将其添加到循环后执行 [htemp release]。addObject 保留并且每个添加的项目将从循环中泄漏。同样,这仅在垃圾收集关闭时才相关。
It's most likely either you aren't created the array correctly or rs doesn't contain what you expect it to contain, and so [rs next] isn't getting called ever (if rs is nil, for example, no iterations of this loop would execute and you wouldn't have any sort of error).
很可能要么您没有正确创建数组,要么 rs 不包含您期望它包含的内容,因此 [rs next] 永远不会被调用(例如,如果 rs 为零,则没有迭代此循环会执行,你不会有任何错误)。

