zoukankan      html  css  js  c++  java
  • 数据存储之plist、偏好设置

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

        // 存储基本类型数据

        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);

  • 相关阅读:
    一文解读RESTful (转)
    一文解读Redis (转)
    一文解读JSON (转)
    一文解读单点登录 (转)
    一文解读雪碧图 (转)
    一文解读骨架屏 (转)
    一文解读MPA/SPA(转)
    一文解读HTTP2 (转)
    一文解读HTTP (转)
    HTML5中Video标签无法播放mp4的解决办法
  • 原文地址:https://www.cnblogs.com/xiangjune/p/4971953.html
Copyright © 2011-2022 走看看