<APP_HOME>:iOS為了保護APP而在每次開啓APP時會給予流水號。
<APP_HOME>/AppName.app:主程式碼,亦稱bundle,不被iTunes備份。
<APP_HOME>/Documents:資料永久存放地,會備份。
<APP_HOME>/Library:非使用者資料相關,會備份。
<APP_HOME>/Library/Preferences:透過NSUserDefaults存放的程式相關設定檔(同Android的SharedPreference),會備份。
<APP_HOME>/Library/Caches:更新時暫存資料區,不備份。
<APP_HOME>/tmp:暫時性資料,不備份。
-----
在Xcode中,黃色的資料夾代表group,表示非實際存在的資料夾。而藍色的資料夾才代表實際存在檔案系統中的資料夾。
取得檔案路徑 -----
取出指定檔案的路徑:
| // pathForResouce為檔名,ofType為檔案型態 [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"png"]; | 
| [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/test/icon.png]; | 
| // inDirectory為資料夾名稱 [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"png" inDirectory:@"test"]; | 
取<APP_HOME>/Documents 內的檔案路徑(simulator與實機取出的路徑不相同):
| NSArray* path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* documentsDirectory = [paths objectAtIndex:0]; | 
取<APP_HOME>/tmp:
| NSTemporaryDirectory(); | 
<APP_HOME>/Library/Preferences 內資料存取 -----
在New File中,開啓Setting Bundle,會出現相關的.plist,裡頭可以增減設定的參數。在程式中呼叫的話是以identifier為key來取得其中的參數,所以identifier不能重覆。
| - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     // Override point for customization after application launch.     [[NSUserDefaults standardUserDefaults] setValue:@"Michael" forKey:@"name_preference"]; //將Michael這個值放入key為name_preference的item中     [[NSUserDefaults standardUserDefaults] synchronize]; //確定放入,同Android中的SharedPreference的commit方法     NSLog(@"%@", [[NSUserDefaults standardUserDefaults] stringForKey:@"name_preference"]);     return YES; } | 
Property List存取 -----
若要存的文字檔並不是很大(不超過1MB,通常用來存取遊戲中的點數)時,將內容存在Property List中。
開啟繼承UIViewController的類別
| - (void)viewDidLoad {     [super viewDidLoad];  // Do any additional setup after loading the view, typically from a nib.     // 將內容存入Array中,記得最後需加上nil表示array結束     NSArray* myData = [NSArray arrayWithObjects:@"first", @"second", @"third", nil];     // 將Document的路徑取出     NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);     NSString* documentsDirectory = [paths objectAtIndex:0];     // 加上檔案名稱。stringByAppendingPathComponent方法是將後面的參數加上"/",好處是如果今天是window系統時,會自動換成"\"     NSString* filePath = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];     // 寫入檔案     [myData writeToFile:filePath atomically:YES]; } | 
加入一個button並寫入動作
| - (IBAction)readProperty:(id)sender {// 將Document的路徑取出     NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);     NSString* docDirectory = [paths objectAtIndex:0];     // 讀出檔案內資料     NSString* filePath = [docDirectory stringByAppendingPathComponent:@"data.plist"];     NSArray* dataArray = [NSArray arrayWithContentsOfFile:filePath];     NSLog(@"%@", dataArray); } | 
資料出處《Object-C與iOS開發入門》
 
沒有留言:
張貼留言