1
1

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 3 years have passed since last update.

Xcodeでの外部ファイルの扱い方

Posted at

Xcodeで外部ファイルにアクセスするには?

Xcodeで作成したアプリは、通常設定では、外部ファイルにアクセスできません。
そこで、Xcodeの設定をいじって、外部ファイルにアクセスする方法を紹介します。

#手取り早く紹介
##まずはXcodeに権限を与える
スクリーンショット 2020-05-09 19.59.18.png
環境設定のセキュリティとプライバシーを選択し、フルディスクアクセスにXcodeを加えます。

##次にXcode側の設定を行う
スクリーンショット 2020-05-09 20.04.13.png
Xcodeを開き、コーディング画面を開いたら、「〜.entitlements」を開き、
スクリーンショット 2020-05-09 20.04.24.png
App SandboxのValueを「NO」にします。

##完了!
あとは、Coding!
csvファイルを読み込みたいときは、

Swift
var str = ""
var importURL:URL = URL(fileURLWithPath: "絶対パス.csv")
        do {
            str = try String( contentsOf: importURL, encoding: String.Encoding.utf8 )
        } catch {
            print(error)
        }

csvファイルを書き出したいときは、

Swift
var exportURL:URL = URL(fileURLWithPath: "絶対パス.csv")
do {
    let exportString = "ハンターハンターは、いつ再開するのでしょうか?"
    try exportString.write(to: exportURL, atomically: true, encoding: .utf8)
} catch {
    print(error)
}

こんな感じで書けると思います。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?