1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

XCodeでサブディレクトリに配置したファイルが読み込めない場合の対処メモ

Last updated at Posted at 2017-04-30

やりたいこと

サーバーサイドの環境が、開発環境、ステージング環境、本番環境など複数用意されている。
アプリ側から見た接続先が複数に分かれるので、ソースコードにべた書きするのではなく property list に外だしして、動的に読み分けたい。

こんな感じで property listを用意して
NPUrl_swift.png

こんな感じで読み込みが行われる。

let path = bundle.path(forResource: "dev1",
                       ofType:"plist",
                       inDirectory:"Properties")
let dict = NSDictionary.init(contentsOfFile: path!)!

はまったこと

上記の pathnil になってしまう。

原因と対処方法

プロジェクトに各ファイルは追加されていたが、ディレクトリが追加されていないので読み込みに失敗していた。

  1. まず、プロジェクト内の好きな位置にディレクトリを Finderで 作成する
  2. 左ペインから Add files to プロジェクト名 を選択し、ディレクトリ を追加する
  3. 読み込まれたディレクトリに、好きな property list を追加する

XCodeの左ペインには通常 黄色い ディレクトリが表示されますが、上記手順で追加すると 青い ディレクトリになります(上記キャプチャは正しい手順で登録した状態なので青い)。 inDirectory は青いディレクトリしか読み込まれない、というのが今回の原因でした。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?