你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> ios–把數據存儲到沙盒中

ios–把數據存儲到沙盒中

編輯:關於IOS

把數據存儲到沙盒中
先找到沙盒的路徑,用如下代碼

NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSAllDomainsMask, YES);
NSString *path = [arr objectAtIndex:0];

如果把數據存儲在.txt文件中路徑可變為pStr如下:

NSString *pStr = [path stringByAppendingString:@“/data.txt"];

把一些數據存儲進去(如:hello,world!):

NSString *string = @“hello,world!”;

用歸檔存儲如下:

NSData *data=[[NSData alloc]init];
data=[NSKeyedArchiver archivedDataWithRootObject:string];
[data1 writeToFile:pStr atomically:YES];

讀取保存在沙盒中的數據

NSData *data1=[NSData dataWithContentsOfFile:pStr];
NSArray *array=[NSKeyedUnarchiver unarchiveObjectWithData:data1];
[string addObjectsFromArray:array];

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved