//DataSerialization.h 파일
#import <Foundation/Foundation.h>
#define kFilename @"data.plist" //경로지정
@interface DataSerialization : NSObject
-(NSString*)dataFilePath;
-(NSMutableArray*)loadData;
-(BOOL)saveData:(NSMutableArray*)getArray;
@end
//DataSerialization.m 파일
#import "DataSerialization.h"
#import "VisitMemberAddData.h"
@implementation DataSerialization
-(NSString*)dataFilePath{ //경로 설정 안해도됨....
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentDirectory = [paths objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:kFilename];
}
-(NSMutableArray*)loadData
{
NSString* filePath = [self dataFilePath];
NSArray* array = nil;
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
array = [NSArray arrayWithContentsOfFile:filePath];
}
NSMutableArray* returnArray = [NSMutableArray arrayWithCapacity:array.count];
for ( NSData* data in array )
{
VisitMemberAddData* visitData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
[returnArray addObject:visitData];
}
return returnArray;
}
-(BOOL)saveData:(NSMutableArray*)getArray
{
NSMutableArray* array = [NSMutableArray arrayWithCapacity:getArray.count];
for ( VisitMemberAddData* visitData in getArray )
{
NSData* data = [NSKeyedArchiver archivedDataWithRootObject:visitData];
[array addObject:data];
}
return [array writeToFile:[self dataFilePath] atomically:YES];
}
@end
직렬화를 위해 데이터를 변환해야한다.
직렬화 저장 데이터 클래스
//VisitMemberAddData.h 파일
#import <Foundation/Foundation.h>
@interface VisitMemberAddData : NSObject <NSCoding>{
NSString* name;
NSString* type;
NSString* companyName;
NSString* cellPhone;
NSString* visitObject;
}
@property (retain, nonatomic) NSString* name;
@property (retain, nonatomic) NSString* type;
@property (retain, nonatomic) NSString* companyName;
@property (retain, nonatomic) NSString* cellPhone;
@property (retain, nonatomic) NSString* visitObject;
@end
#import "VisitMemberAddData.h"
@implementation VisitMemberAddData
@synthesize name;
@synthesize cellPhone;
@synthesize companyName;
@synthesize type;
@synthesize visitObject;
-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:name forKey:@"name"]; //키값은 모두 다르게 설정해야한다.
[aCoder encodeObject:cellPhone forKey:@"cellPhone"];
[aCoder encodeObject:companyName forKey:@"companyName"];
[aCoder encodeObject:type forKey:@"type"];
[aCoder encodeObject:visitObject forKey:@"visitObject"];
}
-(id)initWithCoder:(NSCoder *)aDecoder{
if(self = [super init]){
self.name = [aDecoder decodeObjectForKey:@"name"];
self.cellPhone = [aDecoder decodeObjectForKey:@"cellPhone"];
self.companyName = [aDecoder decodeObjectForKey:@"companyName"];
self.type = [aDecoder decodeObjectForKey:@"type"];
self.visitObject = [aDecoder decodeObjectForKey:@"visitObject"];
}
return self;
}
@end
사용 예제
DataSerialization* dataSerialization = [[DataSerialization alloc]init];
NSMutableArray* sendArray = [[NSMutableArray alloc]init];
sendArray = [dataSerialization loadData]; //저장된 데이터를 불러온다.
int getCount = sendArray.count + memberArray.count;
VisitMemberAddData* getData = [sendArray objectAtIndex:i];
//데이터를 저장한다.
if ( [dataSerialization saveData:memberArray] )
{
NSLog(@"Success");
}
else {
NSLog(@"Fail");
}
'iPhone' 카테고리의 다른 글
테이블 뷰 만들기 (0) | 2012.05.24 |
---|---|
텍스트뷰 포커스 이동 이벤트, 텍스트뷰 클릭 이벤트 (0) | 2012.05.24 |
UIPopoverController 사용 예제 (0) | 2012.05.11 |
lable NSString 만큼 늘리기, lable 폰트 조절 (0) | 2012.05.11 |
타겟 액션 (Target - Action) (0) | 2011.09.19 |