Programming/iOS
plist를 이용한 데이터 관리
알레아
2011. 7. 19. 17:35
반응형
각각의 view에 데이터를 컨트롤 하기 힘들 때 다음과 같이 plist를 이용하여 데이터를 관리합니다.
아래의 예제를 통해서 plist를 관리하는 법을 간단하게 보겠습니다.
다음과 같이 두개의 탭이 존재하는 프로그램입니다.
second 에서 데이터를 입력 후 저장하게 되면 first 탭에서 동일한 내용의 데이터가 보여집니다.
경로 검색
plist 생성
데이터 읽기
데이터 쓰기
아래의 예제를 통해서 plist를 관리하는 법을 간단하게 보겠습니다.
다음과 같이 두개의 탭이 존재하는 프로그램입니다.
second 에서 데이터를 입력 후 저장하게 되면 first 탭에서 동일한 내용의 데이터가 보여집니다.
경로 검색
- (NSString *)getPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"setting.plist"];
return path;
}
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"setting.plist"];
return path;
}
plist 생성
NSError *error;
NSString *path = [self getPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"setting" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
NSString *path = [self getPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"setting" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
데이터 읽기
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:[self getPath]];
//load from savedStock example int value
self.pName.text = [savedStock objectForKey:@"strName"];
self.pAddress.text = [savedStock objectForKey:@"strAddress"];
[self.pMarried setOn:[[savedStock objectForKey:@"nMarried"] intValue]];
self.pMemo.text = [savedStock objectForKey:@"strMemo"];
[savedStock release];
//load from savedStock example int value
self.pName.text = [savedStock objectForKey:@"strName"];
self.pAddress.text = [savedStock objectForKey:@"strAddress"];
[self.pMarried setOn:[[savedStock objectForKey:@"nMarried"] intValue]];
self.pMemo.text = [savedStock objectForKey:@"strMemo"];
[savedStock release];
데이터 쓰기
NSString *path = [self getPath];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
//here add elements to data file and write data to file
[data setObject:[self.pName.text copy] forKey:@"strName"];
[data setObject:[self.pAddress.text copy] forKey:@"strAddress"];
[data setObject:[NSNumber numberWithInt:self.pMarried.on] forKey:@"nMarried"];
[data setObject:[self.pMemo.text copy] forKey:@"strMemo"];
[data writeToFile:path atomically:YES];
[data release];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
//here add elements to data file and write data to file
[data setObject:[self.pName.text copy] forKey:@"strName"];
[data setObject:[self.pAddress.text copy] forKey:@"strAddress"];
[data setObject:[NSNumber numberWithInt:self.pMarried.on] forKey:@"nMarried"];
[data setObject:[self.pMemo.text copy] forKey:@"strMemo"];
[data writeToFile:path atomically:YES];
[data release];
반응형