XcodeでPlistを利用したデータ管理:Plistファイルの読み込み
iPhoneアプリでXML形式のファイルを扱う方法の一つとして、Plist(プロパティリスト)を利用したデータ管理を行います。
Plistファイルの作成
- 「File」メニューから⇒「New」⇒「File…」を選択。
- 以下のテンプレート選択画面から「Resource」⇒「Property List」を選択します。
- Xcodeのワークウィンドウから作成したPlistファイルを選択して表示します。
-
Plistファイルに必要な情報「Key」と「Value」を追加していきます。
Plistファイルの読み込み
「Key」と「Value」を追加したPlistファイルの読み込みを行うため、プログラムを追記します。
NSBundle* bundle = [NSBundle mainBundle]; //読み込むファイルパスを指定 NSString* path = [bundle pathForResource:@"hoge" ofType:@"plist"]; NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:path]; NSArray *items =[NSArray arrayWithArray:[dic objectForKey:@"Root"]]; for(NSDictionary* str in items){ NSLog(@"%@",[str objectForKey:@"japanese"]); NSLog(@"%@",[str objectForKey:@"english"]); }
プログラムの実行結果は以下になります。
コメントをどうぞ