筆記一下modal的部分:
1. 製作singleton 2. 用NSBoundle開啟plist
1. Singleton:保證一個class內只有一個instance,就是保證在App執行的期間這個class只有一個instance
+ (DataSource *)sharedDataSource
{
static dispatch_once_t once;
static DataSource *sharedDataSource;
dispatch_once(&once, ^ {
sharedDataSource = [[self alloc] init];
});
return sharedDataSource;
}
上面的code會判斷記憶體內是否已經存在此objective,如果存在則直接回傳此instance,如果沒有就直接創建一個instance
如果要用到此instance則可以用:
[DataSource sharedDataSource]; //回傳DataSource的instance
2. 用NSBoundle開plist:
分成兩個步驟:
a. 把要讀的plist檔放到project中
b. 存到NSArray
NSString *path = [[NSBundle mainBundle] pathForResource:@"citylist" ofType:@"plist"];
NSArray list = [NSArray arrayWithContentsOfFile: path];
使用方式:
NSDictionary *dict = [list objectAtIndex:i];
沒有留言:
張貼留言