標籤:
**plist檔案其實就是XML文檔,只是尾碼名為plist。
如果對象是NSString、NSDictionary、NSArray、NSData、NSNumber等類型,就可以使用writeToFile:atomically:方法直接將對象寫到屬性列表檔案中
**
關於plist檔案的寫入
- (void)saveArray{ // 1.獲得沙箱根路徑 NSString *home = NSHomeDirectory(); // 2.document路徑 NSString *docPath = [home stringByAppendingPathComponent:@"Documents"]; // 3.建立資料 NSArray *data = @[@"jack", @10, @"ffdsf"]; NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"]; [data writeToFile:filepath atomically:YES];}
關於plist檔案的讀取
- (IBAction)read { // 1.獲得沙箱根路徑 NSString *home = NSHomeDirectory(); // 2.document路徑 NSString *docPath = [home stringByAppendingPathComponent:@"Documents"]; // 3.檔案路徑 NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"]; // 4.讀取資料 NSArray *data = [NSArray arrayWithContentsOfFile:filepath]; NSLog(@"%@", data);}
【iOS開發】資料持久化之plist儲存