LoginSignup
8
10

More than 5 years have passed since last update.

playground でファイル読み込みをする (画像・テキストファイルなど)

Last updated at Posted at 2015-11-26

playground 内に Resources というディレクトリがあるので、そこにファイルを置いてあげればアクセスできます!

ディレクトリがない場合

Resources ディレクトリがない場合は作りましょう。

playground -> "パッケージの内容を表示" -> Resources ディレクトリ作成

または

mkdir 作業中のplayground.playground/Resources

読み込み方

// 画像
let image = UIImage(named: "sample")

// テキスト
let path = NSBundle.mainBundle().pathForResource("sample", ofType: "json")
let contents = try! String(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
8
10
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
10