定義如何創建一個類的@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