归档

作者: b2a58867a9b2 | 来源:发表于2016-08-08 17:49 被阅读0次

    1.将某个对象写入文件时会调用*在这个方法中说清楚哪些属性需要存储

    - (void)encodeWithCoder:(NSCoder*)encoder

    {

    [encoderencodeObject:self.noforKey:@"no"];

    [encoderencodeInt:self.ageforKey:@"age"];

    [encoderencodeDouble:self.heightforKey:@"height"];

    }

    2.*从文件中解析对象时会调用在这个方法中说清楚哪些属性需要存储

    - (id)initWithCoder:(NSCoder*)decoder

    {

    if(self= [superinit]) {

    //读取文件的内容

    self.no= [decoderdecodeObjectForKey:@"no"];

    self.age= [decoderdecodeIntForKey:@"age"];

    self.height= [decoderdecodeDoubleForKey:@"height"];

    }

    returnself;

    }

    // 2.归档模型对象

    // 2.1.获得Documents的全路径

    NSString*doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];

    // 2.2.获得文件的全路径

    NSString*path = [docstringByAppendingPathComponent:@"stu.data"];

    // 2.3.将对象归档

    [NSKeyedArchiverarchiveRootObject:stutoFile:path];

    NSString*doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];

    // 2.获得文件的全路径

    NSString*path = [docstringByAppendingPathComponent:@"stu.data"];

    // 3.从文件中读取MJStudent对象

    MJStudent*stu = [NSKeyedUnarchiverunarchiveObjectWithFile:path];

    相关文章

      网友评论

          本文标题:归档

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