【发布时间】:2010-01-25 22:49:53
【问题描述】:
我进行了相当多的搜索,但并没有真正找到我的问题的答案,所以希望有人能指出我正确的方向
我是 Objective C 的新手,在执行一些我认为很简单的事情时遇到了一个小问题;从类方法返回一个对象的 NSArray
我有以下类和相关的类方法
@implementation Sale
@synthesize title = _title;
@synthesize description = _description;
@synthesize date = _date;
+(NSArray*)liveSales
{
NSArray *liveSales = [[NSArray alloc] init];
for(int i = 0; i < 100; i++)
{
Sale *s = [[Sale alloc] init];
[s setTitle:[NSString stringWithFormat:@"Sale %d", i+1]];
[s setDescription:[NSString stringWithFormat:@"Sale %d descriptive text", i+1]];
[liveSales addObject:s];
[s release];
s = nil;
}
return [liveSales autorelease];
}
@end
我有一个带有以下代码的 ViewController(为便于阅读而修剪):
@implementation RootViewController
@synthesize saleList = _saleList;
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
[[self saleList] setArray:[Sale liveSales]];
}
我遇到的问题是 saleList 的计数始终为空,因此似乎没有设置数组。如果我调试代码并进入类方法 liveSales,则返回时数组中有正确数量的对象
谁能指出我正确的方向?
谢谢:)
戴夫
【问题讨论】:
-
你对
@interface中的saleList的定义是什么?你从编译器得到什么警告?
标签: iphone objective-c