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];
  • 相关阅读:
    [转]顶点数据压缩
    [转]将某个Qt4项目升级到Qt5遇到的问题
    「05」回归的诱惑:一文读懂线性回归
    AI漫谈:我们距离实现《庆余年》里的五竹叔机器人还有多远?
    “木兰”去哪儿了?被全国700所中小学引入的国产编程语言“木兰”,为何在官网删除了下载链接
    有哪些让人相见恨晚的Python库(一)
    2019年最值得关注的AI领域技术突破及未来展望
    为什么样本方差的分母是n-1?为什么它又叫做无偏估计?
    「04」机器学习、深度学习需要哪些数学知识?
    「03」机器学习、深度学习该怎样入门?
  • 原文地址:https://www.cnblogs.com/isItOk/p/4628716.html
Copyright © 2011-2022 走看看