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];

    }

     

  • 相关阅读:
    MyBatis 处理sql中的 大于,小于,大于等于,小于等于
    以当前日期为时间轴 计算15 天的日期 和15 天后的日期
    java 常用时间操作类,计算到期提醒,N年后,N月后的日期
    用户号已经存在是否覆盖解决办
    List<bean> 转换成List<Map>
    feig中调用其他微服务接口无反应
    从实体类中取值 ,获取修改记录信息,保存修改记录信息
    java遍历实体类的属性和值
    从数据库将数据导出到excel表格
    树同构模板
  • 原文地址:https://www.cnblogs.com/fan-cong/p/5142316.html
Copyright © 2011-2022 走看看