美文网首页
plist文件简单使用 - iOS

plist文件简单使用 - iOS

作者: HAKA | 来源:发表于2017-05-12 13:51 被阅读21次

    // 新建文件创建plist文件
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"banks" ofType:@"plist"];
    //存储类型要对应 不然取出为null
    NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
    NSLog(@"plist文件%@", data);
    NSDictionary *dic = data[1];
    NSString * bankcode = dic[@"bankcode"];
    NSString * bankeng = dic[@"bankeng"];
    NSString * bankname = dic[@"bankname"];
    NSString * img = dic[@"img"];

    NSLog(@"=====%@====%@====%@===%@",bankcode,bankeng,bankname,img);
    
    控制台输出.png 单个数据
    //代码创建plist文件
    // 沙盒路径
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //完整路径
    NSString *doucumentPath = [path objectAtIndex:0];
    NSString *plistPath = [doucumentPath stringByAppendingPathComponent:@"test.plist"];
    
    
    //创建数据
    NSMutableDictionary * testDic = [NSMutableDictionary dictionary];
    [testDic setObject:@"奥迪" forKey:@"车"];
    [testDic setObject:@"红色" forKey:@"车色"];
    [testDic setObject:@"京A888888" forKey:@"车牌"];
    [testDic setObject:@"奥迪A8" forKey:@"车型"];
    //写入数据
    [testDic writeToFile:plistPath atomically:YES];
    
    
    NSMutableDictionary * dic11 = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
    NSLog(@"==车===%@==车色==%@+===车牌=%@===车型=%@",dic11[@"车"],dic11[@"车色"],dic11[@"车牌"],dic11[@"车型"]);
    
    控制台

    相关文章

      网友评论

          本文标题:plist文件简单使用 - iOS

          本文链接:https://www.haomeiwen.com/subject/xzbztxtx.html