码迷,mamicode.com
首页 > 其他好文 > 详细

数据存储之plist、偏好设置

时间:2015-11-17 16:57:40      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

// 偏好设置---------------------------------

    // 存储基本类型数据

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:@"hello world!" forKey:@"testsavekey"];

    // 强制保存,否则需要等系统保存,时间不定

    [defaults synchronize];

    

    // 取数据

    NSString *testStr = [defaults objectForKey:@"testsavekey"];

    NSLog(@"testStr = %@", testStr);

    

    // 数据存储plist-----------------------------

    // Document路径获取

    NSArray *documentPathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentPath = [documentPathArray firstObject];

    NSLog(@"document path = %@", documentPath);

    NSString *filePath = [documentPath stringByAppendingPathComponent:@"test.plist"];

    

    // 数据写入文件

    NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithCapacity:1];

    [dic setObject:@"hello" forKey:@"key1"];

    [dic setObject:@"world !" forKey:@"key2"];

    [dic writeToFile:filePath atomically:YES];

    

    // 读取数据

    NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:filePath];

    NSLog(@"readDic = %@", readDic);

数据存储之plist、偏好设置

标签:

原文地址:http://www.cnblogs.com/xiangjune/p/4971953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!