입니다. 배열의 객체를 이름으로 검사 할 수 있습니다. 내 앱의 어느 시점에서 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];
}
많이 모두 감사 !
그런 다음 당신은 더 나은 기회가 있고, 코드의 언어에 대한 태그를 추가해야 찾을 실패 NSNotFound에 대해 테스트, 그리고 (말)하고 있는지 확인하여 질문에 답을 얻는다. – Kai
예기치 않은 결과가 발생할 수있는 몇 가지 가능성이 있습니다 :'_regionArray'는'nil' 일 수 있습니다; 또한 각 반복에서'string'의 이전 값을 버리고 새로운 값으로 대체합니다. 잠재적으로'nil '이 될 수 있습니다. 디버거를 사용하여 실행 중에 변수의 실제 값을 찾습니다. – waldrumpus