当前位置:编程学习 > wap >>

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

1.创建一个单例模式


//

//  ios24_saveObjectToFileViewController.h

//  ios24-saveObjectToFile

//

//  Created by  on 13-6-18.

//  Copyright 2013年 __MyCompanyName__. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@inte易做图ce 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)shouldAutorotateToInte易做图ceOrientation:(UIInte易做图ceOrientation)inte易做图ceOrientation

{

    // Return YES for supported orientations

    return (inte易做图ceOrientation != UIInte易做图ceOrientationPortraitUpsideDown);

}

 

@end

 

2.建立一个student类

//

//  Student.h

//  ios24-saveObjectToFile

//

//  Created by  on 13-6-18.

//  Copyright 2013年 __MyCompanyName__. All rights reserved.

//

 

#import <Foundation/Foundation.h>

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

@inte易做图ce 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"];

&n

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,