zoukankan      html  css  js  c++  java
  • 数据存储1

    plist 文件不能存储一些 普通对象。

    如:自己创建一个新的继承制NSObject的类(如A类)。在这个类中写两个属性。 

    因为A类没有 writeToFile这个方法,所以不能够写入到plist文件中。

    这个时候可能会想,是不是把这个类的数据放到字典或者数组中。因为字典或者数组有这个 writeToFile 方法。

    结果是:不能存储。

    注:只要是普通对象,就不能调 writeToFile 方法。

    - (void)saveArray
    {
        // 1.获得沙盒根路径
        NSString *home = NSHomeDirectory();
        
        // 2.document路径
        NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];
        
        // 3.新建数据
    //    MJPerson *p = [[MJPerson alloc] init];
    //    p.name = @"rose";
        NSArray *data = @[@"jack", @10, @"ffdsf"];
        
        
        NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"];
        
        
        [data writeToFile:filepath atomically:YES];
    }
    
    - (IBAction)read {
        // 1.获得沙盒根路径
        NSString *home = NSHomeDirectory();
        
        // 2.document路径
        NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];
        
        // 3.文件路径
        NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"];
        
        // 4.读取数据
        NSArray *data = [NSArray arrayWithContentsOfFile:filepath];
        NSLog(@"%@", data);
    }
  • 相关阅读:
    Java文件之NIO核心组件之三选择器
    plsql备份表---只是表---不包含表数据
    根据id来大量删除数据between
    符号的问题
    excel表格中添加单引号的方法
    oracle中insert 多条数据方法
    sql developer以字段来删除大量数据
    Day 29
    Day 28
    Day 27
  • 原文地址:https://www.cnblogs.com/iOS363536404/p/5217850.html
Copyright © 2011-2022 走看看