数据存储----归档解档

要归档的类型,声明部分 .h

@interface AppList : NSObject

{

 NSInteger app_id; //应用ID  (10010)

    NSString *name;

}

@property(readwrite) NSInteger app_id;

@property(copy,nonatomic) NSString *name;


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

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

-(id)copyWithZone:(NSZone *)zone;


//实现部分  .m

@implementation AppList

@synthesize app_id,name;

-(void)setAppList:(NSDictionary *)dic

{
    [self  setApp_id:[dic  objectForKey:@"app_id"]];

    [self setName:[dic objectForKey:@"name"]];


}

-(id)initWithCoder:(NSCoder *)decoder//解码

{

    if(self=[super init])

    {

        self.app_id=[decoder decodeIntForKey:@"app_id"];

        self.name=[decoder decodeObjectForKey:@"name"];

      }
return self;
}


-(void)encodeWithCoder:(NSCoder *)coder//编码

{

    [coder encodeInt:app_id forKey:@"app_id"];

    [coder encodeObject:name forKey:@"name"];

}


-(id)copyWithZone:(NSZone*)zone

{

    AppList *copy=[[AppList allocWithZone:zone] init];

    copy.app_id=self.app_id;

  

 copy.name=[[self.name copyWithZone:zone] autorelease];

  return copy;
}

-(void)dealloc
{
[name release];
[super dealloc];
}

//具体归档解档操作

  NSMutableData *codingData = [[NSMutableData alloc]init];

    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:codingData];

    [archiver encodeObject:dataArray forKey:key];

    [archiver finishEncoding];

    BOOL result = [codingData writeToFile:filepathatomically:YES];

    [archiver release];

    [codingData release];

    return result;





    NSData *codingData = [[NSData alloc]initWithContentsOfFile:filePath];

    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiveralloc] initForReadingWithData:codingData];

    NSMutableArray *data = [unarchiverdecodeObjectForKey:key];

    [unarchiver finishDecoding];

    [unarchiver release];

    [codingData release];

    return data;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值