2012-10-28 4 views
0

내 .plist를 읽고 싶지만 NSLog는 "(null)"과 같습니다. 여기.plist에서 읽기

NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"data.plist"]; 

NSLog(@"%@", [plistDict objectForKey:@"id"]); 

그리고 내 .plist입니다 : http://cl.ly/image/2e3P3Q1z2Z1T

답변

2

경로가 잘못이 다음은 내 코드입니다. plist가 앱 리소스에 있다면 절대 경로를 얻으려면 NSBundle을 사용해야합니다. 이 앱의 문서 폴더에 있다면

NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; 
NSMutableDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:path] 
           mutableCopy]; 

, 당신은이 작업을 수행 할 수 있습니다

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
NSString *path = [basePath stringByAppendingPathComponent:@"data.plist"]; 
NSMutableDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:path] mutableCopy]; 

참고 PLIST에서로드 사전 또는 배열은 항상 불변이다. 변경 가능한 사본을 만들어야합니다.

+0

내 plist는 ~/Library/Application Support/iPhone 시뮬레이터/iOS/응용 프로그램/MY-APP/Documents_에 있지만이 코드는 작동하지 않습니다. – user1269586

+0

문서 폴더 경로를 가져 오는 방법은 [this] (http://stackoverflow.com/a/6907432/343340)를 참조하십시오. 그리고 내 대답을 업데이 트하십시오. – DrummerB

+0

NSLog는 "(널)"와 동일하다 다시 ... 하지만 내 코드가 멋지다 : '- (무효) viewDidAppear : (BOOL) (YES, NSDocumentDirectory, SUserDomainMask) { 있는 NSArray * 경로 =의 NSSearchPathForDirectoriesInDomains 애니메이션; NSString * basePath = ([경로 개수]> 0)? [경로 objectAtIndex : 0] : 없음; NSString * path = [basePath stringByAppendingPathComponent : @ "data-article-itiap.plist"]; NSMutableDictionary * dict = [[NSDictionary dictionaryWithContentsOfFile : path] mutableCopy]; NSLog (@ "% @", [dict objectForKey : @ "id"]); }' – user1269586