NSMutableArray count always returns zero

前端 未结 3 1598
無奈伤痛
無奈伤痛 2021-01-12 13:09

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 obje

3条回答
  •  生来不讨喜
    2021-01-12 13:21

    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];
    

提交回复
热议问题