无法在Objective C中向NSMutableArray添加对象

时间:2021-02-28 22:24:37

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.

我正在使用iPhone SDK并且遇到一个简单的问题。我试图将NSNumber对象添加到NSMutableArray实例变量。我尝试将NSNumber卡添加到NSMutableArray seenCardsArray,但是没有破坏,它不会被添加到数组中。这是代码。


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

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.

因此,此代码执行,并且似乎没有泄漏(根据Leaks性能工具)。但是,当单步执行代码时,CardIdObject在任何时候都不会出现在seenCardsArray中。

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

通过搜索,我知道这些基本问题对于ObjC新手(像我一样)很常见,所以请提前道歉!

2 个解决方案

#1


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

你有没有初始化你的seenCardsArray?如果不是你需要某个地方 - 这通常在你的类的init方法中完成:

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

Then it is released in the dealloc method:

然后它在dealloc方法中释放:

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

#2


Perspx has outlined one way of initializing the array. However, you can also use the class methods provided by NSArray:

Perspx概述了一种初始化阵列的方法。但是,您也可以使用NSArray提供的类方法:

self. viewedCardsArray = [NSMutableArray array];

This can go in init or elsewhere.

这可以在init或其他地方进行。

Note: The object will be autoreleased.

注意:该对象将被自动释放。

#1


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

你有没有初始化你的seenCardsArray?如果不是你需要某个地方 - 这通常在你的类的init方法中完成:

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

Then it is released in the dealloc method:

然后它在dealloc方法中释放:

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

#2


Perspx has outlined one way of initializing the array. However, you can also use the class methods provided by NSArray:

Perspx概述了一种初始化阵列的方法。但是,您也可以使用NSArray提供的类方法:

self. viewedCardsArray = [NSMutableArray array];

This can go in init or elsewhere.

这可以在init或其他地方进行。

Note: The object will be autoreleased.

注意:该对象将被自动释放。