1
2

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.

Simulaterの中に指定したファイルが存在するか確認してある場合ない場合条件分岐させる方法

Posted at
        do {

            let fileManager = FileManager.default
            let filePath = "ファイルを置いたパス"
            let fileURL = filePath.appendingPathComponent("sample.pdf")

            //ファイルがある場合
            if fileManager.fileExists(atPath: fileURL.path){

            ファイルがある場合の処理

            }else{

            ファイルがない場合の処理

            }
                            
        } catch let error as NSError {
            print("error",error)
        }

 
例えばファイルパスが

/Users/****/Library/Developer/CoreSimulator/Devices/0000/1111/2222/3333/4444/5555/sample.pdf

みたいに長くても全然オッケーです。
 
ダメな例↓

            if fileManager.fileExists(atPath: fileURL) ← .pathなし

 
上記で条件分岐しても何故か返ってくる値がおかしいってなってかなり詰まってたんですが、pathをつけることで解決しました。

同じ問題を抱えて悩んでいる方の参考になれば幸いです。
 

参考記事: [swift] ファイルの存在確認方法
https://www.fixes.pub/program/17881.html

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?