我正在使用 ItemController 来提供要在 tableview 中使用的项目列表。我似乎无法填充控制器,我不知道为什么。
这是控制器类的代码:
。H
#import <Foundation/Foundation.h>
@class Item;
@interface ItemController : NSObject
@property (nonatomic, copy) NSMutableArray *items;
- (NSUInteger)countOfList;
- (Item*)objectInListAtIndex:(NSUInteger)theIndex;
- (void)addItem:(Item *)item;
@end
.m
#import "ItemController.h"
#import "Item.h"
@interface ItemController ()
@end
@implementation ItemController
- (NSUInteger)countOfList {
return [self.items count];
}
- (Item *)objectInListAtIndex:(NSUInteger)theIndex {
return [self.items objectAtIndex:theIndex];
}
- (void)addItem:(Item *)item {
[self.items addObject:item];
}
@end
项目.m
@implementation Item
-(id)initWithName:(NSString *)name{
self = [super init];
if (self) {
_name = name;
return self;
}
return nil;
}
@end
我正在使用以下代码来填充列表:
ItemController* controller = [[ItemController alloc] init];
for (NSString* key in raw_data) {
NSLog(key); // This outputs the keys fine
[controller addItem:[[Item alloc] initWithName:key]];
}
NSLog([NSString stringWithFormat:@"%d",[controller countOfList]]); // Always 0