8
7

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.

[swift] testでpathForResourceが通らない時

Last updated at Posted at 2015-03-07

下記のような書き方でpathを取得していたのだが、テストだと何故かpathがnilになってしまう。

wrong_bundle.swift
let path = NSBundle.mainBundle().pathForResource("configuration",ofType:"json")

ちょっと調べたら、どうもテストではこの方法では取得できないらしい。
ObjectiveCの時から続いていたらしく、ObjectiveCの情報はこちらにありました。
http://qiita.com/hmuronaka/items/4b4a8f38b117e94542b4
これをSwiftでやるコードはこれでした。

correct_bundle.swift
let path = NSBundle(forClass:NameOfClass.self).pathForResource("configuration",ofType:"json")

参考:
http://stackoverflow.com/questions/25651403/swift-equivalent-of-nsbundle-bundleforclassself-class
http://stackoverflow.com/questions/24417182/how-to-use-scnscenesource-method-entrywithidentifierclasstype

8
7
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
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?