ios24-数据持久化-对象归档

1.创建一个单例模式

//

//  ios24_saveObjectToFileViewController.h

//  ios24-saveObjectToFile

//

//  Created by  on 13-6-18.

//  Copyright 2013 __MyCompanyName__. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ios24_saveObjectToFileViewController : UIViewController<UITextFieldDelegate>

{

    UITextField *tfId;

    UITextField *tfName;

    UITextField *tfClass;

}

@property (nonatomic,retain) IBOutlet UITextField *tfId;

@property (nonatomic,retain) IBOutlet UITextField *tfName;

@property (nonatomic,retain) IBOutlet UITextField *tfClass;

-(IBAction)save;

-(IBAction)read;

-(NSString *)getFilePath;

@end


----------------------------------------------


//

//  ios24_saveObjectToFileViewController.m

//  ios24-saveObjectToFile

//

//  Created by  on 13-6-18.

//  Copyright 2013 __MyCompanyName__. All rights reserved.

//


#import "ios24_saveObjectToFileViewController.h"

#import "Student.h"

@implementation ios24_saveObjectToFileViewController

@synthesize tfId,tfName,tfClass;

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.

}


#pragma mark - View lifecycle

//保存数据

-(IBAction)save{

//实现保存

    NSString *savePath=[self getFilePath];

    //定义data

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

    //定义压缩的工具类

    NSKeyedArchiver *archer=[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];

    //保存的对象

    Student *stu=[[Student alloc]init];

    stu.studentId=tfId.text;

    stu.studentName=tfName.text;

    stu.studentClass=tfClass.text;

    //压缩

    

    [archer encodeObject:stu forKey:@"stuobj"];

    [archer finishEncoding];

    //写入文件

    [data writeToFile:savePath atomically:YES];

    NSLog(@"保存成功");

    

    

}

//读取数据

-(IBAction)read{

    //获取path

    NSString *readPath=[self getFilePath];

    //获取文件的二进制流

    NSData *data=[[NSData alloc]initWithContentsOfFile:readPath];

    if (data.length>0) {

        NSKeyedUnarchiver *unArchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:data];

        //完成student对象的实例化

        Student *stu=[unArchiver decodeObjectForKey:@"stuobj"];

        [unArchiver finishDecoding];

        //设置显示

        tfId.text=stu.studentId;

        tfName.text=stu.studentName;

        tfClass.text=stu.studentClass;


    }

    //进行解压

    

}

//

-(NSString *)getFilePath{

    //设置文件保存的路径

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    

    //获取documents路径

    NSString *documentPath = [paths lastObject];

    //定义全路径

    NSString *savePath = [documentPath stringByAppendingPathComponent:@"student.achiver"];

    return savePath;


}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];

    return YES;

}

- (void)viewDidLoad

{

    [super viewDidLoad];

    tfId.delegate=self;

    tfName.delegate=self;

    tfClass.delegate=self;

// Do any additional setup after loading the view, typically from a nib.

}


- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}


- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

}


- (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

}


- (void)viewWillDisappear:(BOOL)animated

{

[super viewWillDisappear:animated];

}


- (void)viewDidDisappear:(BOOL)animated

{

[super viewDidDisappear:animated];

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}


@end


2.建立一个student类

//

//  Student.h

//  ios24-saveObjectToFile

//

//  Created by  on 13-6-18.

//  Copyright 2013 __MyCompanyName__. All rights reserved.

//


#import <Foundation/Foundation.h>

//要实现当前类对象,就必须实现nscoding协议

@interface Student : NSObject<NSCoding>

{

    NSString *studentId;

    NSString *studentName;

    NSString *studentClass;

}

@property (nonatomic,retain) NSString *studentId;

@property (nonatomic,retain) NSString *studentName;

@property (nonatomic,retain) NSString *studentClass;

@end


------------------------------



//

//  Student.m

//  ios24-saveObjectToFile

//

//  Created by  on 13-6-18.

//  Copyright 2013 __MyCompanyName__. All rights reserved.

//


#import "Student.h"


@implementation Student

@synthesize studentId,studentName,studentClass;

//进行归档编码

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

    //将属性

    [aCoder encodeObject:studentId forKey:@"studentId"];

    [aCoder encodeObject:studentName forKey:@"studentName"];

    [aCoder encodeObject:studentClass forKey:@"studentClass"];

}

//对对象进行读取读取出来

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

    //进行属性解码

    self.studentId=[aDecoder decodeObjectForKey:@"studentId"];

    self.studentName=[aDecoder decodeObjectForKey:@"studentName"];

    self.studentClass=[aDecoder decodeObjectForKey:@"studentClass"];

    return self;

}

@end



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值