2012-08-10 5 views
0

plist 파일에서 데이터를 저장/검색하고 있지만 문제가 있습니다. plist 파일이 없으면 만들 수 없습니다. 나는 그것이 응용 프로그램과 함께 문서 폴더에 만들어 질 필요가있다. 내 코드는 내가 문제를 해결하기 위해 이상 검색하지만 기회, 내가 코드에없는 모르는Xcode에서 plist 파일 만들기

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"playersnames.plist"]; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 

if (![fileManager fileExistsAtPath: path]) 
{ 
    path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"playersnames.plist"] ]; 

} 

NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; 


NSFileManager *fileManager2 = [NSFileManager defaultManager]; 
//NSMutableDictionary *data; 

if ([fileManager2 fileExistsAtPath: path]) 
{ 
    data = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 

} 
else 
{ 
    // If the file doesn’t exist, create an empty dictionary 
    data = [[NSMutableDictionary alloc] init]; 

} 

입니다.

다른 사람이 나에게 설명하는 방법을 알려주세요.

내 코드가 viewDidLoad에 있습니다.

미리 감사드립니다.

답변

0

당신은 결코 파일을 생성하지 않습니다 - fileExists이 첫 번째 참조를 변경 : 이것에

if (![fileManager fileExistsAtPath: path]) 
{ 
    path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"playersnames.plist"] ]; 

} 

:

if (![fileManager fileExistsAtPath: path]) 
{ 
    BOOL ret = [[NSDictionary dictionary] writeToFile:path atomically:YES]; 
    assert(ret); // better work! 
} 

이제 파일을 만든 것입니다. 나중에 나중에 동일한 파일에 대한 전체 사전을 저장하여 동일한 작업을 수행해야 할 것입니다. 중요한 참고 사항 : 이러한 호출에서 반환 코드를 테스트하십시오!

+0

감사합니다. – aLFaRSi