Use nskeyedarchiver for archiving

Source: Internet
Author: User

将各种类型的对象存储到文件中,而不仅仅是字符串、数组和字典类型,有一种更灵活的方法。就是利用NSKeyedAarchiver类创建带键(keyed)的档案来完成。

Mac OS X从版本10.2开始支持带键的档案。在此之前,要使用NSArchiver类创建连续的(sequential)归档。连续的归档需要完全按照写入时的顺序读取归档中的数据。

在带键的归档中,每个归档字段都有一个名称。归档某个对象时,会为它提供一个名称,即键。从归档中检索该对象时,是根据这个键来检索的。这样可以按照任意的顺序将对象写入归档并进行检索。另外,如果向类添加了新的实例变量或删除了实例变量,程序也可以进行处理。

注意:默认情况下,只能对NSDate, NSNumber, NSString, NSArray, or NSDictionary来进行归档。

如果要归档我们自定义的对象,在part2会讲解 part1
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){    @autoreleasepool {                //---------------使用NSKeyedArchiver归档(存储各种类型的对象数据)-------------        //将字典数据写到文件glossary.archive中        NSDictionary *glossary = [NSDictionary dictionaryWithObjectsAndKeys:                                  @"A class defined so other class can inherit from it.",@"abstract class",                                  @"To implement all the methods defined in a protocol.",@"adopt",                                  @"Storing an object for later use.",@"archiving",                                  nil                                  ];        [NSKeyedArchiver archiveRootObject:glossary toFile:@"glossary.archive"];                //将文件glossary.archive中的数据读到字典对象并显示出来        NSDictionary *readglossary = [NSKeyedUnarchiver unarchiveObjectWithFile:@"glossary.archive"];                for(NSString *key in readglossary)            NSLog(@"%@: %@",key,[readglossary objectForKey:key]);    }    return 0;}

其中,

    [NSKeyedArchiver archiveRootObject:glossary toFile:@"glossary.archive"];  

将字典glossary写入到文件glossary.archive中。可以为该文件指定任何路径名。在本例中,文件被写入当前目录下。

之后,又通过

    NSDictionary *readglossary = [NSKeyedUnarchiver unarchiveObjectWithFile:@"glossary.archive"];  

方法将创建的归档文件读入执行程序中。这个语句将指定的文件打开并读取文件的内容,该文件必须是以前归档操作的结果。可以为文件指定完整路径名或相对路径名。

在显示结果之后,可以简单的通过枚举其内容来验证恢复是否成功。

原文http://blog.csdn.net/enuola/article/details/7802371

part2

但是,当我们对自己定义的对象进行“编码/解码”操作时,却需要实现NSCoding协议的相关方法来告诉程序如何来“编码/解码”我们自己的对象! NSCoding协议的方法:

- (void)encodeWithCoder:(NSCoder *)aCoder;

- (id)initWithCoder:(NSCoder *)aDecoder;

 那么,我们就对类实现“编码/解码”协议:在AddressCard.h中,申明实现NSCoding协议:

@interface AddressCard : NSObject<NSCopying,NSCoding>

 在AddressCard.m中,实现NSCoding协议的编码/解码 方法:

#pragma mark- NSCoding

- (void)encodeWithCoder:(NSCoder *)aCoder{

    [aCoder encodeObject:self.name forKey:@"AddressCard_name"];

    [aCoder encodeObject:self.email forKey:@"AddressCard_email"];

    [aCoder encodeInt32:self.salary forKey:@"AddressCard_salary"];

 

}

- (id)initWithCoder:(NSCoder *)aDecoder{

    _name=[[aDecoder decodeObjectForKey:@"AddressCard_name"] retain];

    _email=[[aDecoder decodeObjectForKey:@"AddressCard_email"] retain];

    _salary=[aDecoder decodeInt32ForKey:@"AddressCard_salary"];

    return self;

 

}

这样,我们就能够归档自己定义的类对象。 

NSString *filePhyName=[filePath stringByAppendingPathComponent:@"ObjectFile"];

    BOOL isSuccess=NO;

    isSuccess= [NSKeyedArchiver archiveRootObject:objArray toFile:filePhyName];

    if (isSuccess) {

        NSLog(@"Success");

    }else{

        NSLog(@"False");

    }

    

    // 反归档

    NSMutableArray *myObj=[NSKeyedUnarchiver unarchiveObjectWithFile:filePhyName];

    for (AddressCard *theCard in myObj) {

        [theCard print];

    }

  从输出可以看到,归档成功! 归档需要注意的是:1.同一个对象属性,编码/解码的key要相同!2.每一种基本数据类型,都有一个相应的编码/解码方法。如:encodeObject方法与decodeObjectForKey方法,是成对出现的。3.如果一个自定义的类A,作为另一个自定义类B的一个属性存在;那么,如果要对B进行归档,那么,B要实现NSCoding协议。并且,A也要实现NSCoding 协议。  

使用NSKeyedArchiver归档

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.