Plist 檔案的多次讀寫,實現IOS資料的本地化處理,實現小量資料的本地化處理

來源:互聯網
上載者:User

Plist 檔案的多次讀寫,實現IOS資料的本地化處理,實現小量資料的本地化處理

廢話少說,直接上代碼:

 

 (void)viewDidLoad {    [super viewDidLoad];        //讀取plist檔案在程式檔案夾中的檔案,並且要注意,程式檔案的檔案只能讀不能寫,只能在程式沙箱之中;    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plisttest" ofType:@"plist"];    NSLog(@"這是plist檔案的路徑%@", plistPath);    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];    NSLog(@"這是plist檔案的內容%@", data);            //添加資料說明,可以進行修改;    [data setObject:@"我在東北大學" forKey:@"address"];    [data setObject:@"男" forKey:@"sex"];    //擷取應用沙箱的的DOCUMENT 的路徑;    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString* plistPath1 = [paths objectAtIndex:0];        //在此處設定檔案的名字補全其中的路徑, 注意對於檔案記憶體的修改是在記憶體之中完成的,然後直接把現在的資料一次性更新,這樣減少了檔案的讀寫的次數    NSString *filename =[plistPath1 stringByAppendingPathComponent:@"plisttest.plist"];    NSLog(@"這是沙箱之中的路徑%@", filename);        //寫入檔案    [data writeToFile:filename atomically:YES];        NSLog(@"證明我的資料真正得寫入在其中了");        //證明檔案寫入成功    NSMutableDictionary *afterData = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];    NSLog(@"這是寫入過資料的情況%@", afterData);        NSLog(@"程式結束");        // Do any additional setup after loading the view, typically from a nib.}

運行結果是:

 

 

2015-04-26 12:08:55.857 plist[1702:112934] 這是plist檔案的路徑/Users/xuyaowen/Library/Developer/CoreSimulator/Devices/6BD2A547-CC45-4739-9578-C1D268C26B20/data/Containers/Bundle/Application/28977EA1-0F6A-4B4F-AF37-F6BADEEB889C/plist.app/plisttest.plist2015-04-26 12:08:55.868 plist[1702:112934] 這是plist檔案的內容{    info = "\U8fd9\U662f\U6211\U7684plist\U6587\U4ef6";    name = xiaohongqi;    password = xiaohongqi;}2015-04-26 12:08:55.869 plist[1702:112934] 這是沙箱之中的路徑/Users/xuyaowen/Library/Developer/CoreSimulator/Devices/6BD2A547-CC45-4739-9578-C1D268C26B20/data/Containers/Data/Application/F145B509-73B0-4266-AE06-94F2B0A56A7C/Documents/plisttest.plist2015-04-26 12:08:55.902 plist[1702:112934] 證明我的資料真正得寫入在其中了2015-04-26 12:08:55.902 plist[1702:112934] 這是寫入過資料的情況{    address = "\U6211\U5728\U4e1c\U5317\U5927\U5b66";    info = "\U8fd9\U662f\U6211\U7684plist\U6587\U4ef6";    name = xiaohongqi;    password = xiaohongqi;    sex = "\U7537";}2015-04-26 12:08:55.902 plist[1702:112934] 程式結束


 

然後通過運行結束時的日誌內容,證明成功!

 

用前往檔案夾:複製路徑即可訪問到程式沙箱之中的檔案,注意提高自身的能力!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.