UserDefaults

 

UserDefaults支持的基本数据类型包含:

NSDataNSStringNSNumberNSDateNSArray, or NSDictionary


对于自定义的类,如果直接存储会出现如下错误:

Property list invalid for format (property lists cannot contain objects of type ‘CFType’)


    NSUserDefaults* u=[NSUserDefaults standardUserDefaults];
    
    [u setObject:@"abcdefzxc" forKey:@"key1"];
    
    [u setObject:@[@"ns",@"obj",@"arr"]forKey:@"key2"];
    
    [u synchronize];//保存到本地
    
    
    /////////////////
    
    
    NSString*ss=[u objectForKey:@"key1"];
    NSLog(@"读取文件内容 %@",ss);
    
    NSArray*dats=[u objectForKey:@"key2"];
    NSLog(@"读取文件内容");
    for (NSString *s in dats) {
        NSLog(@"  %@",s);
    }

打印如下

2015-10-10 20:25:12.647 table[9444:698519] 读取文件内容 abcdefzxc
2015-10-10 20:25:12.648 table[9444:698519] 读取文件内容
2015-10-10 20:25:12.648 table[9444:698519]   ns
2015-10-10 20:25:12.648 table[9444:698519]   obj
2015-10-10 20:25:12.648 table[9444:698519]   arr


你可能感兴趣的:(ios)