pch*_*10k 1 iphone cocoa cocoa-touch objective-c
我正在使用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];
}
Run Code Online (Sandbox Code Playgroud)
所以这段代码执行,似乎没有泄漏(根据Leaks性能工具).但是,当单步执行代码时,CardIdObject似乎不会出现在seenCardsArray中.
通过搜索,我知道这些基本问题对ObjC新手(像我一样)很常见,所以请提前道歉!
Ale*_*ski 17
你初个化了viewedCardsArray吗?如果不是你需要某个地方 - 这通常是在init你班级的方法中完成的:
- (id)init
{
self = [super init];
if(self) {
viewedCardsArray = [[NSMutableArray alloc] init];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
然后它在dealloc方法中发布:
- (void)dealloc
{
[viewedCardsArray release];
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16169 次 |
| 最近记录: |