• 文件操作---沙盒路径


         // 1.路径
        // NSString *path = [self getDocumetnPath];
        // 2.创建文件
        // path = [path stringByAppendingString:@"/my.txt"]; // 加/***在路径目录---文件夹下
         // 3.写入文件
         // NSString *name = @"百川";
         // [name writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil]; // 字符串写入沙盒
        
        // 数组写入文件
        /*NSString *path = [self getDocumetnPath];
         NSLog(@"%@",path);
        
         path = [path stringByAppendingString:@"/array.txt"]; // plist---写plist文件
         NSArray *nameArray = @[@"百川",@"隋松",@"徐光耀",@"三毛"];
         [nameArray writeToFile:path atomically:YES];
        */
        // 字典写入文件
        /*
         NSString *path = [self getDocumetnPath];
         NSLog(@"%@",path);
         path = [path stringByAppendingString:@"/dict.plist"]; // 字典写成plist文件
         NSDictionary *dict = @{@"男":@[@"1",@"2",@"6"],@"女":@[@"x",@"y",@"z"]};
        
         [dict writeToFile:path atomically:YES];
        */
        
        // NSData(二进制)写入文件
       /*
        NSString *path = [self getDocumetnPath];
        NSLog(@"%@",path);
        path = [path stringByAppendingString:@"/data"];
        
        NSString *string = @"hehheheh呵呵ehehheehe";
        NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
        
        NSLog(@"%@",data);
        [data writeToFile:path atomically:YES];
        */
        
        // 读取沙盒文件的内容 ****** 内容结构
        /*
         
        NSString *path = [self getDocumetnPath];
        NSLog(@"%@",path);
        path = [path stringByAppendingString:@"/dict.plist"];
        NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:path];
        NSLog(@"%@",readDic);
         
       */
        
        /**
          IOS中只有NSString NSArray NSDictionry NSData(以及他们的可变类)可以直接在沙盒里面写入,其他的不可以***数组 
          字典等容器里面存储的数据必须是上述的数据
         
         */
        
    
        // 复杂对象
       
        /*
        Person *p = [[Person alloc] init];
        p.name = @"三毛";
        p.sex = @"男";
        
        NSString *path = [self getDocumetnPath];
        NSLog(@"%@",path);
        path = [path stringByAppendingString:@"/person"];
        
        // 归档把person存入沙盒中
        // 1.创建一个可变的二进制数据对象
        NSMutableData *data = [NSMutableData data];
        // 2.根据data创建归档对象
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
        // 3.对person进行编码
        [archiver encodeObject:p forKey:@"aperson"];
        // 4.结束编码(归档)
        [archiver finishEncoding]; // 很重要
        // 把data存入沙盒中
        [data writeToFile:path atomically:YES];
       */
     
    
       
       /*
        Teacher *ate = [[Teacher alloc] init];
        ate.name = @"哈哈哈";
        ate.age = @"30";
        
        NSString *path1 = [self getDocumetnPath];
        path1 = [path1 stringByAppendingString:@"/techer"];
        
        NSMutableData *teaData = [NSMutableData data];
        NSKeyedArchiver *teaArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:teaData];
        [teaArchiver encodeObject:ate forKey:@"atecher"];
        [teaArchiver finishEncoding];
        [teaData writeToFile:path1 atomically:YES];
      
        */
        
        
        // 取出teacher对象---反归档
       
        /*
        // 1.找路径
        NSString *path = [[self getDocumetnPath] stringByAppendingString:@"/techer"];
        // 2.根据路取出二进制data
        NSData *data = [NSData dataWithContentsOfFile:path];
        // 3.根据取出的data(二进制数据)---创建NSKeyedUAnrchiver对象
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        // 4.根据归档时的key值拿出teacher对象 ****** 解码******
        Teacher *teacher = [unarchiver decodeObjectForKey:@"atecher"];
        // 5.结束反归档
        [unarchiver finishDecoding];
        
        NSLog(@"%@ %@",teacher.name,teacher.age);
    
         */
  • 相关阅读:
    《锋利的jQuery》补充笔记
    sass学习笔记
    《HTML5与CSS3基础教程》学习笔记 ——补充
    ajax常见问题(部分)
    html新特性(部分)
    less 笔记
    《JavaScript高级程序设计》补充笔记2
    《JavaScript高级程序设计》补充笔记1
    《CSS3秘笈》备忘录
    显示实现接口的好处c#比java好的地方
  • 原文地址:https://www.cnblogs.com/bachl/p/4676448.html
Copyright © 2020-2023  润新知