2010년 08월 25일
코코아 아이폰 개발 - 세이브 화일 저장하기
+(void)saveToFile:(NSString*)filename object:(id)root
{
NSString *path = [self getPath:filename];
// root 부터 줄줄이 하위객체를 다 돌면서 path화일에 저장하게 됩니다.
BOOL saved=[NSKeyedArchiver archiveRootObject:root toFile:path];
if (saved){
NSLog(@"saved");
} else {
NSLog(@"not saved");
};
}
+(id)loadFromFile:(NSString*)filename
{
NSString *path = [self getPath:filename];
// path화일을 읽어서 적절한 객체를 생성해서 넘겨줍니다.
id obj = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
return obj;
}
@interface MyObject : NSObject<NSCoding> {
....
int score;
MySubObject* subObj;
int intArray[INT_ARRAY_MAX];
}
...
@end
@implementation MyObject
- (id) initWithCoder: (NSCoder *)coder
{
if ((self = [super init]))
{
score = [coder decodeIntForKey:@"score"];
self.subObj = [coder decodeObjectForKey:@"subObj"];
[coder decodeArrayOfObjCType:@encode(int) count:INT_ARRAY_MAX at:(void *)intArray];
}
}
- (void) encodeWithCoder: (NSCoder *)coder
{
[coder encodeInt:score forKey:@"score"];
[coder encodeObject:subObj forKey:@"subObj"]; // MySubObject도 NSCoding구현 필수!
[coder encodeArrayOfObjCType:@encode(int) count:INT_ARRAY_MAX at:(const void*)intArray];
}
@end
이 글과 관련있는 글을 자동검색한 결과입니다 [?]
# by | 2010/08/25 22:15 | 개발 | 트랙백 | 덧글(0)





☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]