IOS入门 SandBox Plist NSUserDefault

1 、从应用沙盒 、 plist 、NSUserDefault中读取数据

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *addContent = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];

    // -- 沙盒的文件路径
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [pathArray objectAtIndex:0];
    // -- 文件的全路径
    NSString *filePath = [path stringByAppendingPathComponent:@"user.plist"];
    // -- 将字典提取出来
    NSMutableDictionary *rootArray = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];

    //NSLog(@"%@" , rootArray);

    [rootArray setObject:addContent forKey:@"data"];

    // -- 判断字典中是否存在某个键值
    if(![rootArray objectForKey:@"data1"]){
        [rootArray setObject:addContent forKey:@"data1"];
    }

    // -- 将字典保存到文件中
    [rootArray writeToFile:filePath atomically:YES];
    // -- 再次读出来,对比数据是否写入
    rootArray = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
    //NSLog(@"%@" , rootArray);

    //----------------------------------------------------------------------
    // --
    //
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"userList" ofType:@"plist"];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:bundlePath];
    NSLog(@"%@" , dic);

    [dic setObject:addContent forKey:@"data"];
    [dic writeToFile:bundlePath atomically:YES];
    dic = [NSMutableDictionary dictionaryWithContentsOfFile:bundlePath];
    NSLog(@"%@" , dic);

    //----------------------------------------------------------------------
    // --
    //
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //[defaults setObject:@"value1" forKey:@"valueKey1"];
    NSString *read = [defaults objectForKey:@"valueKey"];
    NSString *read1 = [defaults objectForKey:@"valueKey1"];
    NSLog(@"-- %@ -- %@" , read , read1);
}
时间: 2024-07-31 06:10:22

IOS入门 SandBox Plist NSUserDefault的相关文章

iOS开发读取plist文件、iphone中plist文件的

在Xcode中建立一个iOS项目后,会自己产生一个.plist文件,点击时会看见它显示的是类似于excel表格: 但是,如果打开方式选择Source Code,你会看见它其实是一个xml文件. 我们会做一个小例子,在这个例子中我们自己建立一个plist文件并填入数据,然后运行时读取这个plist文件,并将数据填写在界面上. 首先要知道读取plist文件的方法,一般来说,使用代码 NSString *plistPath = [[NSBundle mainBundle] pathForResourc

iOS学习之 plist文件的读写

在做iOS开发时,经经常使用到到plist文件,  那plist文件是什么呢? 它全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件.属性列表文件的扩展名为.plist ,因此通常被称为 plist文件.文件是xml格式的. Plist文件通经常使用于储存用户设置,也能够用于存储捆绑的信息 我们创建一个项目来学习plist文件的读写. 1.创建项目Plistdemo 项目创建之后能够找到项目相应的plist文件,打开例如以下图所看到的: 在编辑器中显示相似与表

IOS APP配置.plist汇总(转自coolweather )

IOS APP配置.plist汇总(转自coolweather ) 此文转自http://www.cocoachina.com/bbs/read.php?tid=89684&page=1 作者:coolweather 整了一下了,分享給大家 1.     Application does not run in background(键名:UIApplicationExistsOnSuspend)自从iOS4.0之後,當你在應用程式執行的時候按下Home鍵,應用程式並不會中斷目前的執行,而是躲到背

关于iOS 'The sandbox is not sync with the Podfile.lock'问题解决方法

问题描述: github下载的Demo,很多时候使用到CocoaPods,有的时候因为依赖关系或者版本问题不能编译运行.出现例如The sandbox is not sync with the Podfile.lock问题时候,如下所示 diff: /../Podfile.lock: No such file or directory diff: Manifest.lock: No such file or directory error: The sandbox is not in sync

iOS 'The sandbox is not sync with the Podfile.lock'问题解决

问题描述: github下载的Demo,很多时候使用到CocoaPods,有的时候因为依赖关系或者版本问题不能编译运行.出现例如The sandbox is not sync with the Podfile.lock问题时候,如下所示 diff: /../Podfile.lock: No such file or directory diff: Manifest.lock: No such file or directory error: The sandbox is not in sync

iOS \'The sandbox is not sync with the Podfile.lock\'问题解决

iOS \'The sandbox is not sync with the Podfile.lock\'问题解决 HUANGDI 发表于 2015-02-27 09:51:13 问题描述: github下载的Demo,很多时候使用到CocoaPods,有的时候因为依赖关系或者版本问题不能编译运行.出现例如The sandbox is not sync with the Podfile.lock问题时候,如下所示 diff: /../Podfile.lock: No such file or d

IOS开发 - Info.plist跟pch文件的作用

IOS开发 - Info.plist和pch文件的作用 1. Info.plist和pch文件的作用 2. UIApplication的常见使用 3. AppDelegate的代理方法 4. UIApplication , AppDelegate , UIWindow , UIViewController 的关系 5. IOS程序的完整启动过程 ﹣Info.plist文件﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣我是分割线﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣﹣ *** Bundl

ios入门篇 -hello Word(1)

温馨提示:,如果您使用移动终端阅读本篇文章,请连接wifi的情况下阅读,里面有大量图片,以免造成您不必要的损失. 潜水博客园很多年,闲来无事,聊一下自己的经历,语文不好(如有什么错别字,请您在下评论)望您谅解,没有上过什么学的. 博主介绍   2010 入园博客园学习.net,只是为后面做.net开发做铺垫.   2011-2012年 2年一直做.net开发(一直处于打杂状态),由于一直打杂状态,萌发转行做ios的状态.   2013-至今 做过4-5个ios方面的app(现阶段处于打杂状态).

iOS入门学习详解

本文来源:http://www.zretc.com/technologyDetail/447.html ios学习起来并没有想象中的那么困难,只不过是你想要开发ios的话,成本相对比较高吧,在windows里面装虚拟机的时代我是体验过的,那个开发起来简直就是苦不堪言啊,那个宕机的速度真是分分钟的事,reset已经被用的如火纯清了,这样你就需要一台mac,有了mac还不够,总不能全部用模拟器开发吧,像定位啊之类的,还是需要实机来测试的,模拟器跟实机的差别还是挺大的.当然也没必要开发之前就买一个帐号