IOS入门 SandBox Plist NSUserDefault

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
}

 

以上是关于IOS入门 SandBox Plist NSUserDefault的主要内容,如果未能解决你的问题,请参考以下文章

Apple 的 plist 编辑器入门指南:基础操作与高级功能详解

沙盒路径

firebase 快速入门 ios 示例

iOS学习之iOS沙盒(sandbox)机制和文件操作

iOS学习之iOS沙盒(sandbox)机制和文件操作1

新手快速入门教程:Sandbox官方像素画教程2nd