2016-12-22 5 views
0

ソートする必要がある私のプロジェクトのplistファイルからデータを取得したいと思います。今私は、そこから私のplistはNSDictionaryのplistからデータを取得

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<array> 
    <dict> 
     <key>code</key> 
     <string>AF</string> 
     <key>phone_code</key> 
     <string>93</string> 
     <key>name</key> 
     <string>Afghanistan</string> 
    </dict> 
    <dict> 
     <key>code</key> 
     <string>AL</string> 
     <key>phone_code</key> 
     <string>355</string> 
     <key>name</key> 
     <string>Albania</string> 
    </dict> 

のように見え、その上で最後country.And件まで私はこの

NSString* path = [[NSBundle mainBundle] pathForResource:@"CountriesWithPhoneCodes" ofType:@"plist"]; 
    NSArray* a = [NSArray arrayWithContentsOfFile:path]; 
    for (NSDictionary *d in a) 
    { 
     NSString *cdate = [d objectForKey:@"CDate"]; 
     if ([cdate isEqualToString:@"Whatever is currently selected"]) 
     { 
      NSString *text = [d objectForKey:@"Text"]; 
      // do something with text here 
     } 
    } 

    NSDictionary *dict = [[NSDictionary alloc]init]; 
    dict = [self indexKeyedDictionaryFromArray:a]; 

    NSLog(@"This is %@",[[dict allValues]objectAtIndex:0]); 

などのデータを取得しています、私は取得しています出力は

This is { 
    code = AF; 
    name = Afghanistan; 
    "phone_code" = 93; 
} 

ですデータに正しくアクセスできません。値と国名のphone_codeをキーに辞書を作成するにはどうすればいいですか?

+0

アクセスしたいのは?これは{ code = AF; 名前=アフガニスタン; "phone_code" = 93; } –

+0

非常に不明です。 'd'はキー' 'CDate ''を持っていません。あなたは、あなたが望む辞書を見つけるために述語、または 'indexOfObjectPassingTest:'を使うことができます。また、配列を使用する代わりに、国名の統一性があると考えると、代わりに国名をキーとして使用し、トップレベルで辞書を使用することもできます。 – Larme

+0

「データが正しくアクセスできない」とはどういう意味ですか?むしろ、 'name'と 'phone_code'だけを保持する辞書が必要ですか? – dirtydanee

答えて

1

NSPredicateあなたのニーズに完全に合っています。

NSString *path = [[NSBundle mainBundle] pathForResource:@"CountriesWithPhoneCodes" ofType:@"plist"]; 
NSArray *array = [NSArray arrayWithContentsOfFile:path]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"phone_code = %@", @"String variable containing code"]; 
NSDictionary *countryDictionary = [array filteredArrayWithPredicate:predicate].firstObject; 

あなたは、このような構造体で辞書を作成したい場合:{"phone_code":"country_code"}

NSMutableDictionary *phonesDict = [NSMutableDictionary new]; 
for (NSDictionary *dict in array) { 
    NSString *phoneCode = dict[@"phone_code"]; 
    NSString *countryCode = dict[@"country_code"]; 
    phonesDict[countryCode] = phoneCode; 
} 

注:あなたは、ディスク上のファイルへのアクセスを避けるためにどこかにこの辞書を保存する必要があります。

0
NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]]; 

// Your dictionary contains an array of dictionary 
// Now pull an Array out of it. 
NSArray *arrayList = [NSArray arrayWithArray:[dictRoot objectForKey:@"catlist"]]; 

// Now a loop through Array to fetch single Item from catList which is Dictionary 
[arrayList enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) { 
// Fetch Single Item 
// Here obj will return a dictionary 
NSLog(@"Category name : %@",[obj valueForKey:@"category_name"]); 
NSLog(@"Category id : %@",[obj valueForKey:@"cid"]); 
}]; 
関連する問題