LoginSignup
1
0

More than 3 years have passed since last update.

[iOS]自分で作成した.plistファイルから値を取得する方法

Last updated at Posted at 2016-06-03

自分で作成した.plistファイルから値を取得する方法

自分で作成した.plistファイルから呼び出して値を取得するクラスを作成しました。

TSPropertyList.h

//FileType

///plist
extern NSString *const TSFileTypePropertyList;

/**
 * PropertyList取得値 [rootの返り値がNSDictionaryの場合]
 */
+ (id)getPropertyListWithKey:(NSString *)key
                withFileName:(NSString *)fileName;
/**
 * PropertyList取得値 [rootの返り値がNSArrayの場合]
 */
+ (id)getPropertyListWithIdentifier:(NSInteger)identifier
                       withFileName:(NSString *)fileName;

TSPropertyList.m

//FileType

///plist
NSString *const TSFileTypePropertyList = @"plist";

/**
 * PropertyList取得値 [rootの返り値がNSDictionaryの場合]
 *
 * @param key 値取得用のKey
 * @param fileName 値取得用のファイル名
 *
 * @return PropertyList値[任意]
 */
+ (id)getPropertyListWithKey:(NSString *)key
                withFileName:(NSString *)fileName
{
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *filePath = [bundle pathForResource:fileName ofType:TSFileTypePropertyList];

    // ファイルの存在チェック
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSDictionary *propertyList = [NSDictionary dictionaryWithContentsOfFile:filePath];
        return propertyList[key];
    }
    return nil;
}

/**
 * PropertyList取得値[rootの返り値がNSArrayの場合]
 *
 * @param key 値取得用のKey
 * @param fileName 値取得用のファイル名
 * 
 * @return PropertyList値[任意]
 */
+ (id)getPropertyListWithIdentifier:(NSInteger)identifier
                       withFileName:(NSString *)fileName
{
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *filePath = [bundle pathForResource:fileName ofType:TSFileTypePropertyList];

    // ファイルの存在チェック
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSArray *propertyList = [NSArray arrayWithContentsOfFile:filePath];
        return propertyList[identifier];
    }
    return nil;
}

これをプログラム中で呼び出すには、下記のようにする。


Hoge.plistを作成 1plistの追加方法

Hoge.m
#import "TSPropertyList.h"

- (void)hoge
{
    id object1 = [TSPropertyList getPropertyListWithIdentifier:0
                                                  withFileName:@"Hoge"];
    NSLog(@"object1 : %@",object1);
    id object2 = [TSPropertyList getPropertyListWithKey:@"hoge"
                                           withFileName:@"Hoge"];
    NSLog(@"object2 : %@",object2);
 }

plistの追加方法


代入したいディレクトリ選択中に [Command + N] で 新規ファイル を作成

→ [Resouce] -> [Property List] を選択
※ 
ファイル名には必要に応じた適当な名前を付ける。


関連記事

【About】(http://qiita.com/sunstripe) - サンストライプ


制作チーム:サンストライプ

sunstripe_logo.png
http://sunstripe.main.jp/

(月1WEBコンテンツをリリースして便利な世の中を作っていくぞ!!ボランティアプログラマー/デザイナー/イラストレーター/その他クリエイター声優募集中!!)

地域情報 THEメディア

THE メディア 地域活性化をテーマに様々なリリース情報も含め、記事をお届けしてます!!
https://the.themedia.jp/

ゼロからはじめる演劇ワークショップ

多様化の時代に向けて他者理解を鍛える

プログラミングワークショップ・ウェブ塾の開講!!!

様々なテーマでプログラミングに囚われずに取り組んでいきます。
詳しくはこちら ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
プログラミングサロン 月1だけのプログラミング学習塾

協力応援 / 支援者の集い

チーム:サンストライプ

プログラミングラボ

一緒にポートフォリオを作りませんか?現場の体験やそれぞれの立場から年齢関係なく作品を作りたい方々と一緒にチームを作って、作品を作っています。現場に行きたい人には、職場紹介や職場の体験や悩み相談なども受けております。
様々な職種からプログラミングの知識を得たい、デザインの知識を得たい、データーベースの知識を得たいという人が集まっております。
週1のミーティングにそれぞれの近況と作業報告して、たまにリモート飲み会などをしております!!

興味がある方は、DMに話しかけてみてください。

トラストヒューマン

http://trusthuman.co.jp/
私たちは何よりも信頼、人と考えてます。

「コンサルティング」と「クリエイティブ」の両角度から「人材戦略パートナー」としてトータル的にサポートします!!

キャリア教育事業
広域学習支援プラットフォーム『のびのび日和』
https://slc-lab.amebaownd.com/


1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0