这个NSMutableArray出了什么问题?

时间:2014-02-25 22:45:00

标签: ios objective-c

@interface PINQuote : NSObject
@property (nonatomic, strong) NSMutableArray *lines;
@property (nonatomic, strong) NSString *quoteID;
@property (nonatomic, strong) NSString *customerName;
@end

当我尝试:     PINQuote * quote = [[PINQuote alloc] init];     [quote.lines addObject:@“TEST STRING”];

数组仍为零。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

将以下内容添加到实现中:

- (NSMutableArray *)lines
{
    if (!_lines) // Lazy load the mutable array when asked for.
        _lines = [NSMutableArray array];
    return _lines;
}

或者如果您不喜欢延迟加载:

- (id)init
{
    self = [super init];
    if (self) {
        _lines = [NSMutableArray array]; // Eager load the mutable array.
    }
    return self;
}

答案 1 :(得分:2)

您只声明了该属性。你现在必须创建它。尝试延迟实例化:

<强> PINQuote.m

- (NSMutableArray*)lines {
    if (!_lines)
        _lines = [NSMutableArray array];
    return _lines;
}