2013-11-14 4 views
0

여기 내 문제입니다.plist 파일에서 NSDictionary

내 plist 데이터 (JSON에서 직렬화 됨)를 NSDictionary로 변환 할 수 없습니다.

내 응용 프로그램에 데이터를 저장하려면이 방법으로 plist 파일에 쓰고 있습니다.

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
    [dict setObject:[NSNumber numberWithInt:[[MyUser sharedUser] userID]] forKey:@"userID"]; 
    [dict setObject:[[MyUser sharedUser] firstname] forKey:@"firstname"]; 
    [dict setObject:[[MyUser sharedUser] lastname] forKey:@"lastname"]; 
    [dict setObject:[[MyUser sharedUser] mail] forKey:@"mail"]; 
    [dict setObject:[[MyUser sharedUser] pwd] forKey:@"pwd"]; 
    [dict setObject:[[MyUser sharedUser] token] forKey:@"token"]; 
    [dict setObject:[[MyUser sharedUser] picture] forKey:@"picture"]; 

    NSData *putData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil]; 
    NSArray *array = [[NSArray alloc] initWithObjects:putData, nil]; 
    [array writeToFile:[self getSaveFilePath] atomically:YES]; 
} 

내 NSDictionary를 가져 오기 위해 plist에서 데이터를로드하고 싶습니다.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    NSString *myPath = [self getSaveFilePath]; 
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath]; 
    if (fileExists) 
    { 
     NSData *data = [NSData dataWithContentsOfFile:myPath]; 
     NSLog(@"%@", data); 
     NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
     NSLog(@"%@", s); 
    } 
    return YES; 
} 

s이 같다 : 내가있는 NSDictionary로 변환 내 문자열로 무엇을 해야할지하지 않습니다

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<array> 
    <data> 
    eyJsYXN0bmFtZSI6IkRvbm5pbmdlciIsImR0ZWxscyI6IlRPT00iLCJpc0RDYXJkUHJv 
    RGVmYXVsdCI6ImluZGl2aWR1YWwiLCJzZW5kaW5nSW52aXQiOiJzbXMiLCJpc0F1dG9E 
    bCI6IjAiLCJmaXJzdG5hbWUiOiJUaG9tYXMiLCJjb250YWN0TGlzdCI6eyJtZXNzYWdl 
    IjoiUmVjb3JkKHMpIEZvdW5kIiwic3VjY2VzcyI6dHJ1ZSwiZGF0YSI6eyJ0b3RhbENv 
    dW50IjozLC [....] 
</data> 
</array> 
</plist> 

(같은 내가 Fraise에서 열 내 파일 내용 등). plist에서 NSDictionary를 얻으려면 어떻게해야합니까?

당신이있는 NSArray에있는 NSData를 넣고 writeToFile에, 당신은 처음있는 NSArray로 돌아갈 필요가 사용하고있는로
+0

내가 그런 말을하지 않았거나 그 의미하지 않았다. plist에 저장된 데이터는 JSON에 있습니다. 어쨌든 SPA는 해결책을 찾습니다. –

답변

2

, 그 배열

NSArray *array = [NSArray arrayWithContentsOfFile:myPath]; 

NSData *data = array[0]; 
+0

완벽한, 고마워. 나는 배열 [0]을 사용하는 것을 생각하지 않았습니다. –

0

NSArray에서있는 NSData 객체를 가져 및 JSON?

[dict writeToFile:[self getSaveFilePath] atomically:YES]; 

NSDictionary *restoredDictionary = [NSDictionary dictionaryWithContentsOfFile:[self getSaveFilePath]];