2014-03-28 4 views
0

약간의 문제가 발생했습니다. 나는 다음 데이터를 포함하는 .plist 파일을 가지고있다. http://imageshack.com/a/img856/1421/xvzm.png.plist 파일에서 읽기

그래서 나는 배열 읽기보다 첫 번째 배열을 읽어야한다. 아니면 파일을 재 작성하고 사전을 입력 루트 키를 변경하고, 다음과 같이 읽을 필요 :

NSString *errorDesc = nil; 
NSPropertyListFormat format; 
NSString *plistPath; 
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
    NSUserDomainMask, YES) objectAtIndex:0]; 
plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"]; 
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) { 
    plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; 
} 
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath]; 
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization 
    propertyListFromData:plistXML 
    mutabilityOption:NSPropertyListMutableContainersAndLeaves 
    format:&format 
    errorDescription:&errorDesc]; 
if (!temp) { 
    NSLog(@"Error reading plist: %@, format: %d", errorDesc, format); 
} 
self.personName = [temp objectForKey:@"Name"]; 
self.phoneNumbers = [NSMutableArray arrayWithArray:[temp objectForKey:@"Phones"]]; 

답변

1

는 다음과 같은 코드를 수행 할 수 있습니다

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; 
NSArray *dataArray = [NSArray arrayWithContentsOfFile:plistPath]; 

이 배열은 모든 사전을 포함, 당신 그들이 좋아하는 얻을 수 있습니다 :이 제외

NSDictionary *dict = [dataArray objectAtIndex:0]; 
+1

이 존재하는 경우 폴더를 문서에서 파일을로드하는 코드를 던진다. – rmaddy

+1

그리고 실제로 질문에 대답하지 않습니다. 질문은 배열에서 원하는 데이터를 가져 오는 것입니다. – rmaddy

+0

'objectAtIndex :'는 오래된 구문입니다. 새로운 것입니다. –

1
SString *plistFile = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; 
NSArray *array = [NSArray arrayWithContentsOfFile:plistFile]; 

for(NSDictionary *dictionary in array) { 
    NSLog(@"%@", dictionary); 
} 

NSDictionary *item0 = array[0]; 
NSString *imageName = item[0][@"name"]; 
+1

이것은 문서에서 파일을로드하는 코드를 포함하고 있습니다 폴더가있는 경우 OP는 아마 그 코드를 원할 것입니다. – rmaddy

+0

OP 코드는 먼저 Documents 폴더에서 찾은 다음 리소스 번들을 폴백으로 사용합니다. OP를 위해서, 나는이 대답이 그 논리를 제거했다는 것을 지적했다. – rmaddy