2009-09-11 6 views
0

내 .plist가 소스 코드로 viwed되었을 때 XML 파일이 아닌 것처럼 보이는 이유를 잘 모릅니다.plist에서 항목 수 반환

{ 

    authorLastName = Doe; 
    authorFirstName = Jane; 
    imageFilePath = "NoImage.png"; 
    title = "Account Test 1"; 
    year = 2009; 
}, 

{ 

    authorLastName = Doe; 
    authorFirstName = John; 
    imageFilePath = "NoImage.png"; 
    title = "Account Test 2"; 
    year = 2009; 
} 

. 소스 코드로 볼 때 .plist는이 모양이어야합니까?

둘째, 누구나 항목 수를 어떻게 늘릴 수 있습니까? 예를 들어 각 사전의 성을 입력 할 수 있습니까? 나는 이것을 너무나 많은 시간 동안 연구했고, 내 머리카락을 끌어 당기면서 나는 이것이 정확해질 때까지 계속 움직이지 않는다! 내가 시도하는 모든 메소드는 0을 반환합니다. iPhone 프로그래밍에서 정말 새로운 점입니다. 그래서 얻을 수있는 도움에 감사드립니다! 어쩌면 plist를 읽는 완전한 예제가 될까요?

답변

1

.plist 파일은 XML 또는 텍스트의 두 가지 형식을 취할 수 있습니다. 당신이 가지고있는 것은 텍스트 형식이며, 개념적으로 이해하기 쉽습니다. 반면 XML 형식은 ... 장점이 있습니다. (당신이 plist를 만들 때 기본값입니다. 왜 당신이 ' 티).

계산에 대한 질문이 무엇인지 잘 모르겠습니다. 얼마나 많은 항목이 있는지 (2 개) 또는 성의 속성 집합이있는 항목 (이 경우 2 개) 또는 총 항목 수 (10 개)를 묻는 중입니까?

int count = arr.count; 

또는 두 번째 경우 :

int count = 0; 
for (NSDictionary *dict in arr) { 
    NSString *lastName = [dict objectForKey:@"authorLastName"]; 
    if (lastName) { 
     ++count; 
    } 
} 
첫 번째 경우에 시도한다면 어떤 경우

, 당신은,이

NSArray *arr = [NSArray arrayWithContentsOfFile:pathToFile]; 
//Make sure arr is not nil at this point. If it is, there's either a formatting issue with the file, or the file couldn't be found based on the path you gave 

처럼 시작 했죠

또는 세 번째 경우 :

int count = 0; 
for (NSDictionary *dict in arr) { 
    count += dict.count; 
} 
+0

있는 NSArray * = 경로의 NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [경로 objectAtIndex : 0]; 는 NSString * myPlistPath = documentsDirectory \t \t \t \t \t \t stringByAppendingPathComponent "Accounts.plist"@]; NSArray * arr = [NSArray arrayWithContentsOfFile : myPlistPath]; int count = 0; for (NSDictionary * dict in arr) { \t count + = dict.count; } return count; ////////////////////////// 상기 라인 오류 시뮬레이터를 종료 "EXC_BAD_ACCESS" 프로그램 수신 신호. – WrightsCS

0

나는 finnaly가 작동 plist 카운트를 가지고!

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *myPlistPath = [documentsDirectory 
         stringByAppendingPathComponent:@"Accounts.plist"]; 

NSArray *arr = [NSArray arrayWithContentsOfFile:myPlistPath]; 

int count = 0; 
for (NSDictionary *dict in arr) { 
    NSString *lastName = [dict objectForKey:@"authorLastName"]; 
    if (lastName) { 
     ++count; 
    } 
} 
return([NSString stringWithFormat:@"%d Accounts", count]); 
관련 문제