【发布时间】:2015-03-24 12:57:14
【问题描述】:
我有一堂课:
.h
@interface NoticesDataSource : NSObject <UITableViewDataSource>
@property (nonatomic,strong) NSMutableArray *items;
@end
.m
@implementation NoticesDataSource
...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[self items] count];
// return [_items count];
}
- (instancetype)initWithItems:(NSMutableArray *)items {
self = [super init];
[self setItems:items];
// _items = items;
return self;
}
- (instancetype)init {
self = [super init];
[self setItems:[self prepareItems]];
// _items = [self prepareItems];
return self;
}
- (NSMutableArray *)prepareItems {
NSMutableArray *items = [[NSMutableArray alloc] init];
...
[items addObject:item];
...
[items addObject:item];
return items;
}
...
@end
调用tableView:numberOfRowsInSection: 方法时会出现问题。
那时items 是nil。
我做错了什么?
我已经阅读了http://qualitycoding.org/objective-c-init/、https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html 和一些 stackoverflow 的答案,但我就是不知道我的问题的根源是什么。
谢谢。
【问题讨论】:
-
你确定你的初始化器被调用了吗? (设置断点进行检查。)
-
并且
items被定义为“强”。 -
是的,我确定。我明确地打电话给
init,并在那里设置了断点。它被称为。 -
items属性很强大——你可以在.h文件中看到它。
标签: objective-c memory-management