2010年5月15日土曜日

プロパティリストからキーで配列を取得する

プロパティリストからキーを使って配列を取得するには、以下の手順で取得します。

  1.  メインバンドルを取得する
  2. プロパティリストのファイル名からフルパスを取得する
  3. フルパスを指定してプロパティリストの内容をディレクトリとして取得する。
  4. キーを取得する。
  5. キーをつかって、キーに該当する値を配列として取得する

もちろん、初期化時にディレクトリを保持したり、キー配列を保持したり、配列の初期値を入れることは必要です。(1〜3に該当)

例:

- (void)viewDidLoad {
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"statedictionary" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc]initWithContentsOfFile:plistPath];
self.stateZips = dictionary;
[dictionary release];
NSArray *components = [self.stateZips allKeys];
NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];
self.states = sorted;
NSString *selectedState = [self.states objectAtIndex:0];
NSArray *array = [stateZips objectForKey:selectedState];
self.zips = array;
}

0 件のコメント:

コメントを投稿