定义如何创建一个类的@implementation的方法,而不在@interface定义它?创建的实现方法没有头
例如,我有一个构造函数,做一些初始化,然后从文件中读取数据。我想将文件读取代码分解为一个单独的方法,然后在构造函数中调用它。我不想在头文件来定义这种方法,因为它是公开的,只有这@implementation上下文。
这可能吗?
这是我的例子。我有一个小程序,它是从文件中读取Todo任务列表的。
这里是@interface:
@interface TDTaskList : NSObject {
NSString* name; // The name of this list.
NSMutableArray* tasks; // The set of tasks in this list.
}
-(id)initListOfName:(NSString*)aName;
-(NSArray*)loadListWithName:(NSString*)aName;
@end
这里是@implementation的一部分:
-(id)initListOfName:(NSString*)aName {
if (self = [super init]) {
name = aName;
NSArray* aTasks = [self loadListWithName:aName];
tasks = [NSMutableArray arrayWithArray:aTasks];
}
return self;
}
-(NSArray*)loadListWithName:(NSString*)aName {
// TODO This is a STUB till i figure out how to read/write from a file ...
TDTask* task1 = [[TDTask alloc] initWithLabel:@"Get the Milk."];
TDTask* task2 = [[TDTask alloc] initWithLabel:@"Do some homework."];
return [NSArray arrayWithObjects:task1, task2, nil];
}
我想要做的是不已经确定在以下接口:
-(NSArray*)loadListWithName:(NSString*)aName;
这是正确的答案。 – mxcl 2009-06-23 16:29:56