2014-02-25 58 views
-5
@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」];這個NSMutableArray有什麼問題?

該數組仍然爲零。

任何想法?

+6

當你的對象初始化它不會奇蹟般地創造本身。您需要爲* array *賦值。 –

回答

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; 
} 
2

你只申報財產。您現在必須創建它。嘗試延遲實例:

PINQuote.m

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