2011년 5월 23일 월요일

iphone Archiving 을 이용한 저장로딩

//
//  SampleDay6_1ViewController.m
//  SampleDay6-1
//
//  Created by bit on 11. 5. 23..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SampleDay6_1ViewController.h"
#import "FourLines.h"

@implementation SampleDay6_1ViewController



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

NSString *filePath = [self dataFilePath];
if( [[NSFileManager defaultManager] fileExistsAtPath:filePath]){
// NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
// field1.text = [array objectAtIndex:0];
// field2.text = [array objectAtIndex:1];
// field3.text = [array objectAtIndex:2];
// field4.text = [array objectAtIndex:3];
//
// [array release];
FourLines *fourLines = [NSKeyedUnarchiver unarchiveObjectWithFile:[self dataFilePath]];
field1.text = fourLines.field1;
field2.text = fourLines.field2;
field3.text = fourLines.field3;
field4.text = fourLines.field4;
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[field1 release];
field1 = nil;
[field2 release];
field2 = nil;
[field3 release];
field3 = nil;
[field4 release];
field4 = nil;
}


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

-(NSString *)dataFilePath {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [ paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"file.arc"];
}
-(void)applicationWillResignActive:(NSNotification *)notification
{
// NSMutableArray *array = [[NSMutableArray alloc] init];
//
// [array addObject:field1.text];
// [array addObject:field2.text];
// [array addObject:field3.text];
// [array addObject:field4.text];
// [array writeToFile:[self dataFilePath] atomically:YES];
// [array release];
FourLines *fourLines = [[FourLines alloc] init];
fourLines.field1 = field1.text;
fourLines.field2 = field2.text;
fourLines.field3 = field3.text;
fourLines.field4 = field4.text;
[NSKeyedArchiver archiveRootObject:fourLines toFile:[self dataFilePath]];
}

@end


//
//  FourLines.h
//  SampleDay6-1
//
//  Created by bit on 11. 5. 23..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import

#define kField1Key @"key1"
#define kField2Key @"key2"
#define kField3Key @"key3"
#define kField4Key @"key4"


@interface FourLines : NSObject {
NSString *field1;
NSString *field2;
NSString *field3;
NSString *field4;

}

@property (nonatomic, retain) NSString *field1;
@property (nonatomic, retain) NSString *field2;
@property (nonatomic, retain) NSString *field3;
@property (nonatomic, retain) NSString *field4;


-(void)encodeWithCoder:(NSCoder *)encoder;
-(id)initWithCoder:(NSCoder *)decoder;


@end


//
//  FourLines.m
//  SampleDay6-1
//
//  Created by bit on 11. 5. 23..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FourLines.h"


@implementation FourLines

@synthesize field1;
@synthesize field2;
@synthesize field3;
@synthesize field4;


- (void) dealloc
{
self.field1 = nil;
self.field2 = nil;
self.field3 = nil;
self.field4 = nil;
[super dealloc];
}


-(void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:field1 forKey:kField1Key];
[encoder encodeObject:field2 forKey:kField2Key];
[encoder encodeObject:field3 forKey:kField3Key];
[encoder encodeObject:field4 forKey:kField4Key];
}


-(id)initWithCoder:(NSCoder *)decoder {
if( self = [super init] ) {
field1 = [[decoder decodeObjectForKey:kField1Key] retain];
field2 = [[decoder decodeObjectForKey:kField2Key] retain];
field3 = [[decoder decodeObjectForKey:kField3Key] retain];
field4 = [[decoder decodeObjectForKey:kField4Key] retain];
}
return self;
}
@end

댓글 없음:

댓글 쓰기

참고: 블로그의 회원만 댓글을 작성할 수 있습니다.