zoukankan      html  css  js  c++  java
  • 对象类型的本地写入---plist文件创建以及读取

        

    #pragma mark---------------------------写入-------------------------

        

        BPOrderInfo *orderInfo = [[BPOrderInfo alloc] init];

         orderInfo.productName = @"100金币";

        NSString *orderStr = [NSString stringWithFormat:@"%@",orderInfo];

        

        NSMutableArray *array = [NSMutableArray array];

     

        for (int i = 0; i < 5; i++) {

            

            [array addObject:orderStr];

        }

        

        

        NSString *library = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];

        

        library = [library  stringByAppendingFormat:@"/Caches"];

        NSFileManager *manger = [NSFileManager defaultManager];

        NSString  *path = [library stringByAppendingPathComponent:@"TransactionReceipt"];

        

        [manger createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];

         NSString *string = [NSString stringWithFormat:@"%@/%@",path,@"Array.txt"];

    //     [manger createFileAtPath:string contents:nil attributes:nil];

        

       BOOL XXx = [array writeToFile:string atomically:YES];

     

         NSLog(@"写入路径path = %@",string);

         NSLog(@"写入数组array = %@",array);

        

        

        

     

    #pragma mark--------------------------------取出-----------------------

        

        NSString *library2 = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)  firstObject];

        library2 = [library2  stringByAppendingFormat:@"/Caches"];

        NSString *path2= [NSString stringWithFormat:@"%@/%@/%@",library2,@"TransactionReceipt",@"Array.txt"];

        

        NSLog(@"取出路径path = %@",path2);

       

        

        

        NSFileManager *manger2 = [NSFileManager defaultManager];

        

        if ([manger2 fileExistsAtPath:path2]) {

        

            NSArray *arr = [NSArray arrayWithContentsOfFile:path2];

            NSLog(@"发现本地文件");

             NSLog(@"取出数组arr = %@",arr);

       

            for (BPOrderInfo *order in arr) {

                

    //         NSLog(@"order ==== %@",order.productName);

                

            }

            

         }else

         {

            NSLog(@"未发现本地文件");

         }

     

    //    [self proprtyListTest];

        

    }

     

     // plist文件

    -(void)proprtyListTest

    {

        

        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

        

        NSString *path = [documentPath stringByAppendingPathComponent:@"userInfo.plist"];

     

        NSLog(@"propertyList = %@",path);

        NSMutableDictionary *userDic = [NSMutableDictionary dictionary];

        [userDic setObject:@"operation" forKey:@"action"];

        [userDic setObject:@"chen" forKey:@"name"];

        NSMutableArray *array = [NSMutableArray arrayWithObjects:@"张",@"王", nil];

        [userDic setObject:array forKey:@"user"];

        

        NSDictionary *dic = [NSDictionary dictionaryWithObject:@"二十四" forKey:@"江湖"];

        [array addObject:dic];

        [userDic writeToFile:path atomically:YES];

    }

     

  • 相关阅读:
    oracle分页查询sql写法
    查询一张表的id,不在另外一张表的某个字段的数据
    PowerDesigner 使用详解
    java JUC多线程高并发编程
    npm node-sass安装失败解决方法
    Vue中展示二级路由的默认模块 若依框架 自定义指令权限的使用说明
    vue 2.6以后slot 的变化使用
    java中 List<? extends T>与List<? super T> 表示的意思和区别
    java springboot自定义注解 和 时间加一天 一个月 一年方式
    js中||与&&的用法以及every()与some()的用法
  • 原文地址:https://www.cnblogs.com/fan-cong/p/5142316.html
Copyright © 2011-2022 走看看