2012-11-27 2 views
1

입니다. 배열의 객체를 이름으로 검사 할 수 있습니다. 내 앱의 어느 시점에서 plist에 액세스하여 이름으로 찾은 객체 아래에 저장된 정보를 가져와야합니다. 개체를 배열에 배치 할 정수 번호가 없기 때문에이 작업을 수행해야하는 이유가 있습니다.배열의 객체를 이름으로 검색하면

저는 일반적인 절차를 시작했지만 어디에도 없습니다. 어쩌면 그 그냥 내 PLIST 전혀 도움이되지 않고 지금까지

Array 
Dictionary 
     title ... 
     text ... 
     ect ... 
Dictionary 
     title ... 
     text ... 
     ect ... 
    Dictionary 
     title ... 
     text ... 
     ect ... 

내 코드를 보는 방법

이가 .... 너무 늦게 나를 위해 조금 받고. 나는 틀린 일을 분명히하고있다.

-(NSString*)nameFromPlist:(NSString*)name { 

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

NSString *string = [[NSString alloc]init]; 

for(NSDictionary *dict in _regionArray) { 
    string = [[dict objectForKey:name] valueForKey:@"title"]; 

} 

return [NSString stringWithFormat:@"%@",string]; 

}

내 문자열은 null을 반환합니다.

아무도 아이디어가 없습니까? 감사합니다.

답변 :

- (있는 NSString *) nameFromPlist : 여기

모두 코드입니다 (있는 NSString *) 이름 {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *tmpFileName = [[NSString alloc] initWithFormat:@"Orte.plist"]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:tmpFileName]; 

NSString *string; 
NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path]; 

NSDictionary *dict = [[NSDictionary alloc] init]; 
NSString *title; 
for (int i=0; i<[array count]; i++) { 
    dict = [array objectAtIndex:i]; 
    title = [dict valueForKey:@"title"]; 

    if([title isEqualToString:name]) { 
     string = [dict valueForKey:@"title"]; 
     NSLog(@"Titel: %@", string); 
    }   
} 

return [NSString stringWithFormat:@"%@",string]; 

}

많이 모두 감사 !

+2

그런 다음 당신은 더 나은 기회가 있고, 코드의 언어에 대한 태그를 추가해야 찾을 실패 NSNotFound에 대해 테스트, 그리고 (말)하고 있는지 확인하여 질문에 답을 얻는다. – Kai

+0

예기치 않은 결과가 발생할 수있는 몇 가지 가능성이 있습니다 :'_regionArray'는'nil' 일 수 있습니다; 또한 각 반복에서'string'의 이전 값을 버리고 새로운 값으로 대체합니다. 잠재적으로'nil '이 될 수 있습니다. 디버거를 사용하여 실행 중에 변수의 실제 값을 찾습니다. – waldrumpus

답변

0

먼저 NSString을 할당 할 필요가 없습니다. 왜냐하면 valueForKey와는 다른 것을 얻을 것이기 때문입니다.

그렇다면 올바른 코드를 얻으려는 것이 NSPredicate 또는 블록 기반 indexOfObjectPassingTest을 사용하는 것일 수 있습니다. 두 요소 모두 배열을 반복하고 요소에 대한 테스트를 수행하여 일치하는 요소 (또는 요소의 인덱스)를 가져옵니다.

+0

감사합니다. 나는 마침내 그것을 알아 냈다. 원래 질문에 코드를 삽입했습니다! – freshking

+0

좋아요! 아직도 나는 더 효율적인 NSPredicates 또는 블록 기반 API를 살펴 보길 권한다. :) –

0
NSString* plistPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"<PlistFileName>" ofType:@"plist"]; 
NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath]; 
NSMutableArray* titles = [[NSMutableArray alloc] init]; 
for (NSDictionary* d in array) 
    [titles addObject:[d objectForKey:@"title"]]; 
NSLog(@"Object found at index %i", [a indexOfObject:@"<Your object name>"]); 

이 0

관련 문제