2014-01-31 3 views
0

В настоящее время я показываю свой plist, используя код ниже, который работает, однако это кажется мне неэффективным, и я полагаю, что существует более чистый подход.Отображение plist на основе переменной

if ([self.title isEqual: @"How to wear a Kilt"]){ 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"wearAKilt" ofType:@"plist"]; 
    // Load the file content and read the data into arrays 
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; 
    thumbImg = [dict objectForKey:@"thumbImg"]; 
    stepLabel = [dict objectForKey:@"stepLabel"]; 
    descLabel = [dict objectForKey:@"descLabel"]; 
    } 
    // Find out the path of recipes.plist 
    else if ([self.title isEqual: @"How to tie a cravat"]){ 
     NSString *path = [[NSBundle mainBundle] pathForResource:@"wearACravat" ofType:@"plist"]; 
     // Load the file content and read the data into arrays 
     NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; 
     thumbImg = [dict objectForKey:@"thumbImg"]; 
     stepLabel = [dict objectForKey:@"stepLabel"]; 
     descLabel = [dict objectForKey:@"descLabel"]; 
    } 
    else if ([self.title isEqual: @"How to wear a sporran"]){ 
     NSString *path = [[NSBundle mainBundle] pathForResource:@"wearASporran" ofType:@"plist"]; 
     // Load the file content and read the data into arrays 
     NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; 
     thumbImg = [dict objectForKey:@"thumbImg"]; 
     stepLabel = [dict objectForKey:@"stepLabel"]; 
     descLabel = [dict objectForKey:@"descLabel"]; 
    } 

Я попытался с помощью, если заявление только на * пути, но это (как и ожидалось) выдает ошибку undeclared identifier path.

if ([self.title isEqual: @"How to wear a Kilt"]){ 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"wearAKilt" ofType:@"plist"]; 
    } else if ([self.title isEqual: @"How to tie a cravat"]) { 
     NSString *path = [[NSBundle mainBundle] pathForResource:@"wearACravat" ofType:@"plist"]; 
    } 

Любые предложения?

ответ

2

Вы второй бит кода, просто сделать:

NSString *path; 

if ([self.title isEqual: @"How to wear a Kilt"]){ 
    path = [[NSBundle mainBundle] pathForResource:@"wearAKilt" ofType:@"plist"]; 
} else if ([self.title isEqual: @"How to tie a cravat"]) { 
    path = [[NSBundle mainBundle] pathForResource:@"wearACravat" ofType:@"plist"]; 
} 

Можно также использовать словарь для сопоставления с входящей строки в соответствующем имени файла ...

например

NSDicttionary *mapping = @{ 
    @"How to wear a Kilt" : @"wearAKilt", 
    @"How to tie a cravat" : @"wearACravat", 
}; 

Какой может быть статическое определение и, вероятно, следует учитывать локализацию. Затем:

NSString *name = [mapping objectForKey:self.title]; 
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"plist"]; 
+0

сейчас я чувствую себя идиотом. Просто. Из интереса есть хорошая документация о том, как «использовать словарь для сопоставления из входящей строки в соответствующее имя файла» – user2966586

+0

im, устанавливая заголовок на основе представления таблицы из-за использования локалей. – user2966586

+0

Добавлен быстрый текст кода. Ключи должны быть связаны с тем, как вы идеально настроили название ... – Wain

Смежные вопросы