zoukankan      html  css  js  c++  java
  • ios 保存本地数据的方法

    1。
    NSString *path = [[NSBundle mainBundle] pathForResource:@"文件名" ofType:@"plist"];
    // 文件数据类型是array
    NSArray *array=[NSArray arrayWithContentsOfFile:path];
    //文件数据类型是*dictionary
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
     
     
    //1. 创建一个plist文件
        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
        NSString *path=[paths    objectAtIndex:0];
        NSLog(@"path = %@",path);
        NSString *filename=[path stringByAppendingPathComponent:@"test.plist"];    
        NSFileManager* fm = [NSFileManager defaultManager];
        [fm createFileAtPath:filename contents:nil attributes:nil];        
        //NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
        
        //创建一个dic,写到plist文件里
        NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:@"sina",@"1",@"163",@"2",nil];
        [dic writeToFile:filename atomically:YES];
        
        //读文件
        NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];
        NSLog(@"dic is:%@",dic2);
     
     

    2。NSUserDefaults的使用


    创建一个user defaults方法有多个,最简单得快速创建方法:
       NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];

    添加数据到 user defaults:
       [accountDefaults setObject:nameField.text forKey:UserDefaultNameKey];
    也可以添加基本数据类型int, float, bool等,有相应得方法

       [accountDefaults setBool:YES forKey:UserDefaultBoolKey];

    从user defaults中获取数据:

       [accountDefaults objectForKey:NCUserDefaultNameKey]

      
     [accountDefaults boolForKey: UserDefaultBoolKey];

    要点: NSUserDefaults非常好用,并不需要用户在程序中设置NSUserDefaults的全局变量,需要在哪里使用NSUserDefaults的数据,那么就在哪里创建一个NSUserDefaults对象,然后进行读或者写操作。
         针对同一个关键字对应的对象或者数据,可以对它进行重写,重写之后关键字就对应新的对象或者数据,旧的对象或者数据会被自动清理。
     
    3 关于NSUserDefaults保存不了数据的问题If you terminate your app by pressing the home button (in the Simulator or on the device), your User Defaults will get saved.

    如果你按HOME键终止你的应用(真机或者模拟器上),你的值是会被保存的。

    If you terminate your app by pressing "Stop" in Xcode (in the Simulator or on the device), your User Defaults might get saved, but there's a good chance they won't. NSUserDefaults persists any changes periodically, and if you terminate the process before they've been persisted, they'll be gone. You can force the save by calling:

    [[NSUserDefaults standardUserDefaults] synchronize];
     
    如果你在XCODE中,终止你的应用(在模拟器或者真机上),你的值有时候(terminate the process before they've been persisted(不知道是个什么鬼))不会被保存。但是你可以使用以下代码强制保存:
    [[NSUserDefaults standardUserDefaults] synchronize];
  • 相关阅读:
    hbase伪分布式安装
    按照《权威指南》的例子求最低温度并且修改默认调度器为FairScheduler
    利用hadoop1.x集群进行探索性实验(四)~(五)【重复格式化NN后,DN不能启动】【控制namenode检查点发生的频率】
    利用hadoop1.x集群进行探索性实验(三)【修改HDFS块大小】
    利用hadoop1.x集群进行探索性实验(二)【模拟namenode崩溃,通过secondary namenode恢复namenode】
    利用hadoop1.x集群进行探索性实验(一)【对WEB监控界面增加安全机制】
    本地编译64位hadoop并进行部署。
    hadoop完全分布式安装
    storm集群和zookeeper集群的部署过程
    查看电脑连接过的wifi密码
  • 原文地址:https://www.cnblogs.com/isItOk/p/4628716.html
Copyright © 2011-2022 走看看