无法在Objective C中向NSMutableArray添加对象 [英] Having trouble adding objects to NSMutableArray in Objective C

查看:126
本文介绍了无法在Objective C中向NSMutableArray添加对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是iPhone SDK,并且有一个问题很简单。我正在尝试向 NSMutableArray 实例变量添加 NSNumber 对象。我尝试添加NSNumber card 到NSMutableArray viewedCardsArray ,但不会中断,它不会添加到数组。这是代码。

I am using the iPhone SDK and have an issue doing something simple. I am trying to add an NSNumber object to an NSMutableArray instance variable. I tried adding NSNumber card to NSMutableArray viewedCardsArray, however without breaking, it does not get added to the array. Here is the code.


/////////////////////////////////////////////////////
// Inside the header file Class.h
@interface MyViewController : UIViewController {
   NSMutableArray *viewedCardsArray;
   //snip ...
}
@property (nonatomic, retain) NSMutableArray *viewedCardsArray;
@end

/////////////////////////////////////////////////////
// Inside the methods file Class.m
#import "StudyViewController.h"

@implementation StudyViewController
@synthesize viewedCardsArray
  //snip ...

- (IBAction)doShowCard {
   //snip ...
   NSNumber *cardIdObject = [[NSNumber alloc] initWithInt:(int)[self.currentCard cardId]];
   [viewedCardsArray addObject: cardIdObject];
   [cardIdObject release];
}

所以这段代码执行,似乎没有泄漏性能工具)。但是,在逐步执行代码时, CardIdObject 没有显示在 viewingCardsArray 中。

So this code executes, and does not seem to leak (according to the Leaks performance tool). However when stepping through the code, at no point does CardIdObject appear in viewedCardsArray.

知道这些基本问题是很常见的ObjC新手(像我)提前道歉!

Looking through SO, I know these basic questions are pretty common to ObjC newbies (like me) so apologies in advance!

推荐答案

c $ c> viewCardsArray ?如果不是你需要的地方 - 这通常是在你的类的 init 方法中完成:

Have you initialized your viewedCardsArray? If not you need to somewhere - this is usually done in the init method for your class:

- (id)init
{
    self = [super init];
    if(self) {
        viewedCardsArray = [[NSMutableArray alloc] init];
    }
    return self;
}

然后在 dealloc 方法:

- (void)dealloc
{
    [viewedCardsArray release];
    [super dealloc];
}

这篇关于无法在Objective C中向NSMutableArray添加对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆