How to check if a file exists in the Documents directory in Swift
?
如何在Swift文件目录中检查文件是否存在?
I am using [ .writeFilePath ]
method to save an image into the Documents directory and I want to load it every time the app is launched. But I have a default image if there is no saved image.
我正在使用[. writefilepath]方法将图像保存到Documents目录中,我想在每次启动应用程序时加载它。但是如果没有保存图像,我有一个默认的图像。
But I just cant get my head around how to use the [ func fileExistsAtPath(_:) ]
function. Could someone give an example of using the function with a path argument passed into it.
但是我不知道怎么使用[func fileExistsAtPath(_:)]函数。是否有人能给出一个使用带有传入路径参数的函数的例子。
I believe I don't need to paste any code in there as this is a generic question. Any help will be much appreciated.
我认为我不需要在那里粘贴任何代码,因为这是一个通用的问题。如有任何帮助,我们将不胜感激。
Cheers
干杯
8 个解决方案
#1
155
Swift 4.x version
斯威夫特4。x版本
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
if let pathComponent = url.appendingPathComponent("nameOfFileHere") {
let filePath = pathComponent.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath) {
print("FILE AVAILABLE")
} else {
print("FILE NOT AVAILABLE")
}
} else {
print("FILE PATH NOT AVAILABLE")
}
Swift 3.x version
斯威夫特3。x版本
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = URL(fileURLWithPath: path)
let filePath = url.appendingPathComponent("nameOfFileHere").path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath) {
print("FILE AVAILABLE")
} else {
print("FILE NOT AVAILABLE")
}
Swift 2.x version, need to use URLByAppendingPathComponent
斯威夫特2。x版本,需要使用URLByAppendingPathComponent
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
let filePath = url.URLByAppendingPathComponent("nameOfFileHere").path!
let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(filePath) {
print("FILE AVAILABLE")
} else {
print("FILE NOT AVAILABLE")
}
#2
33
Check the below code:
检查下面的代码:
Swift 1.2
斯威夫特1.2
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let getImagePath = paths.stringByAppendingPathComponent("SavedFile.jpg")
let checkValidation = NSFileManager.defaultManager()
if (checkValidation.fileExistsAtPath(getImagePath))
{
println("FILE AVAILABLE");
}
else
{
println("FILE NOT AVAILABLE");
}
Swift 2.0
斯威夫特2.0
let paths = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
let getImagePath = paths.URLByAppendingPathComponent("SavedFile.jpg")
let checkValidation = NSFileManager.defaultManager()
if (checkValidation.fileExistsAtPath("\(getImagePath)"))
{
print("FILE AVAILABLE");
}
else
{
print("FILE NOT AVAILABLE");
}
#3
21
Nowadays (2016) Apple recommends more and more to use the URL related API of NSURL
, NSFileManager
etc.
现在(2016)苹果公司建议越来越多的使用NSURL、NSFileManager等的URL相关API。
To get the documents directory in iOS and Swift 2 use
使用iOS和Swift 2获取文档目录
let documentDirectoryURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory,
inDomain: .UserDomainMask,
appropriateForURL: nil,
create: true)
The try!
is safe in this case because this standard directory is guaranteed to exist.
试一试!在这种情况下是安全的,因为这个标准目录保证存在。
Then append the appropriate path component for example an sqlite
file
然后附加适当的路径组件,例如一个sqlite文件
let databaseURL = documentDirectoryURL.URLByAppendingPathComponent("MyDataBase.sqlite")
Now check if the file exists with checkResourceIsReachableAndReturnError
of NSURL
.
现在检查这个文件是否存在NSURL的checkresourceis可及andreturnerror。
let fileExists = databaseURL.checkResourceIsReachableAndReturnError(nil)
If you need the error pass the NSError
pointer to the parameter.
如果需要错误,将NSError指针传递给参数。
var error : NSError?
let fileExists = databaseURL.checkResourceIsReachableAndReturnError(&error)
if !fileExists { print(error) }
Swift 3:
斯威夫特3:
In Swift 3 checkResourceIsReachable
is marked as can throw
在Swift 3, checkresourceisable被标记为可以抛出。
do {
let fileExists = try databaseURL.checkResourceIsReachable()
// handle the boolean result
} catch let error as NSError {
print(error)
}
To consider only the boolean return value and ignore the error use the nil-coalescing operator
要只考虑布尔返回值并忽略错误,请使用尼尔-合并操作符
let fileExists = (try? databaseURL.checkResourceIsReachable()) ?? false
#4
13
It's pretty user friendly. Just work with NSFileManager's defaultManager singleton and then use the fileExistsAtPath()
method, which simply takes a string as an argument, and returns a Bool, allowing it to be placed directly in the if statement.
非常用户友好。只需使用NSFileManager的defaultManager singleton,然后使用fileExistsAtPath()方法,该方法只接受一个字符串作为参数,并返回一个Bool,允许它直接放在if语句中。
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentDirectory = paths[0] as! String
let myFilePath = documentDirectory.stringByAppendingPathComponent("nameOfMyFile")
let manager = NSFileManager.defaultManager()
if (manager.fileExistsAtPath(myFilePath)) {
// it's here!!
}
Note that the downcast to String isn't necessary in Swift 2.
注意,在Swift 2中,向下对字符串的转换不是必需的。
#5
5
An alternative/recommended Code Pattern in Swift 3 would be:
Swift 3中另一种/推荐的代码模式是:
- Use URL instead of FileManager
- 使用URL代替FileManager
-
Use of exception handling
使用异常处理
func verifyIfSqliteDBExists(){ let docsDir : URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let dbPath : URL = docsDir.appendingPathComponent("database.sqlite") do{ let sqliteExists : Bool = try dbPath.checkResourceIsReachable() print("An sqlite database exists at this path :: \(dbPath.path)") }catch{ print("SQLite NOT Found at :: \(strDBPath)") } }
#6
4
For the benefit of Swift 3 beginners:
为了Swift 3初学者的利益:
- Swift 3 has done away with most of the NextStep syntax
- Swift 3取消了下一个步骤的大部分语法
- So NSURL, NSFilemanager, NSSearchPathForDirectoriesInDomain are no longer used
- 所以NSURL NSFilemanager NSSearchPathForDirectoriesInDomain不再被使用
- Instead use URL and FileManager
- 而是使用URL和FileManager
- NSSearchPathForDirectoriesInDomain is not needed
- 不需要NSSearchPathForDirectoriesInDomain
- Instead use FileManager.default.urls
- 而不是使用FileManager.default.urls
Here is a code sample to verify if a file named "database.sqlite" exists in application document directory:
这里有一个代码示例,用于验证一个名为“database”的文件。sqlite“存在于应用文档目录中:
func findIfSqliteDBExists(){
let docsDir : URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let dbPath : URL = docsDir.appendingPathComponent("database.sqlite")
let strDBPath : String = dbPath.path
let fileManager : FileManager = FileManager.default
if fileManager.fileExists(atPath:strDBPath){
print("An sqlite database exists at this path :: \(strDBPath)")
}else{
print("SQLite NOT Found at :: \(strDBPath)")
}
}
#7
2
Very simple: If your path is a URL instance convert to string by 'path' method.
非常简单:如果您的路径是一个URL实例,通过“path”方法将其转换为字符串。
let fileManager = FileManager.default
var isDir: ObjCBool = false
if fileManager.fileExists(atPath: yourURLPath.path, isDirectory: &isDir) {
if isDir.boolValue {
//it's a Directory path
}else{
//it's a File path
}
}
#8
0
You must add a "/" slash before filename, or you get path like ".../DocumentsFilename.jpg"
您必须在文件名之前添加“/”斜杠,否则您将获得“…/DocumentsFilename.jpg”之类的路径。
#1
155
Swift 4.x version
斯威夫特4。x版本
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
if let pathComponent = url.appendingPathComponent("nameOfFileHere") {
let filePath = pathComponent.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath) {
print("FILE AVAILABLE")
} else {
print("FILE NOT AVAILABLE")
}
} else {
print("FILE PATH NOT AVAILABLE")
}
Swift 3.x version
斯威夫特3。x版本
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = URL(fileURLWithPath: path)
let filePath = url.appendingPathComponent("nameOfFileHere").path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath) {
print("FILE AVAILABLE")
} else {
print("FILE NOT AVAILABLE")
}
Swift 2.x version, need to use URLByAppendingPathComponent
斯威夫特2。x版本,需要使用URLByAppendingPathComponent
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
let filePath = url.URLByAppendingPathComponent("nameOfFileHere").path!
let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(filePath) {
print("FILE AVAILABLE")
} else {
print("FILE NOT AVAILABLE")
}
#2
33
Check the below code:
检查下面的代码:
Swift 1.2
斯威夫特1.2
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let getImagePath = paths.stringByAppendingPathComponent("SavedFile.jpg")
let checkValidation = NSFileManager.defaultManager()
if (checkValidation.fileExistsAtPath(getImagePath))
{
println("FILE AVAILABLE");
}
else
{
println("FILE NOT AVAILABLE");
}
Swift 2.0
斯威夫特2.0
let paths = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
let getImagePath = paths.URLByAppendingPathComponent("SavedFile.jpg")
let checkValidation = NSFileManager.defaultManager()
if (checkValidation.fileExistsAtPath("\(getImagePath)"))
{
print("FILE AVAILABLE");
}
else
{
print("FILE NOT AVAILABLE");
}
#3
21
Nowadays (2016) Apple recommends more and more to use the URL related API of NSURL
, NSFileManager
etc.
现在(2016)苹果公司建议越来越多的使用NSURL、NSFileManager等的URL相关API。
To get the documents directory in iOS and Swift 2 use
使用iOS和Swift 2获取文档目录
let documentDirectoryURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory,
inDomain: .UserDomainMask,
appropriateForURL: nil,
create: true)
The try!
is safe in this case because this standard directory is guaranteed to exist.
试一试!在这种情况下是安全的,因为这个标准目录保证存在。
Then append the appropriate path component for example an sqlite
file
然后附加适当的路径组件,例如一个sqlite文件
let databaseURL = documentDirectoryURL.URLByAppendingPathComponent("MyDataBase.sqlite")
Now check if the file exists with checkResourceIsReachableAndReturnError
of NSURL
.
现在检查这个文件是否存在NSURL的checkresourceis可及andreturnerror。
let fileExists = databaseURL.checkResourceIsReachableAndReturnError(nil)
If you need the error pass the NSError
pointer to the parameter.
如果需要错误,将NSError指针传递给参数。
var error : NSError?
let fileExists = databaseURL.checkResourceIsReachableAndReturnError(&error)
if !fileExists { print(error) }
Swift 3:
斯威夫特3:
In Swift 3 checkResourceIsReachable
is marked as can throw
在Swift 3, checkresourceisable被标记为可以抛出。
do {
let fileExists = try databaseURL.checkResourceIsReachable()
// handle the boolean result
} catch let error as NSError {
print(error)
}
To consider only the boolean return value and ignore the error use the nil-coalescing operator
要只考虑布尔返回值并忽略错误,请使用尼尔-合并操作符
let fileExists = (try? databaseURL.checkResourceIsReachable()) ?? false
#4
13
It's pretty user friendly. Just work with NSFileManager's defaultManager singleton and then use the fileExistsAtPath()
method, which simply takes a string as an argument, and returns a Bool, allowing it to be placed directly in the if statement.
非常用户友好。只需使用NSFileManager的defaultManager singleton,然后使用fileExistsAtPath()方法,该方法只接受一个字符串作为参数,并返回一个Bool,允许它直接放在if语句中。
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentDirectory = paths[0] as! String
let myFilePath = documentDirectory.stringByAppendingPathComponent("nameOfMyFile")
let manager = NSFileManager.defaultManager()
if (manager.fileExistsAtPath(myFilePath)) {
// it's here!!
}
Note that the downcast to String isn't necessary in Swift 2.
注意,在Swift 2中,向下对字符串的转换不是必需的。
#5
5
An alternative/recommended Code Pattern in Swift 3 would be:
Swift 3中另一种/推荐的代码模式是:
- Use URL instead of FileManager
- 使用URL代替FileManager
-
Use of exception handling
使用异常处理
func verifyIfSqliteDBExists(){ let docsDir : URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let dbPath : URL = docsDir.appendingPathComponent("database.sqlite") do{ let sqliteExists : Bool = try dbPath.checkResourceIsReachable() print("An sqlite database exists at this path :: \(dbPath.path)") }catch{ print("SQLite NOT Found at :: \(strDBPath)") } }
#6
4
For the benefit of Swift 3 beginners:
为了Swift 3初学者的利益:
- Swift 3 has done away with most of the NextStep syntax
- Swift 3取消了下一个步骤的大部分语法
- So NSURL, NSFilemanager, NSSearchPathForDirectoriesInDomain are no longer used
- 所以NSURL NSFilemanager NSSearchPathForDirectoriesInDomain不再被使用
- Instead use URL and FileManager
- 而是使用URL和FileManager
- NSSearchPathForDirectoriesInDomain is not needed
- 不需要NSSearchPathForDirectoriesInDomain
- Instead use FileManager.default.urls
- 而不是使用FileManager.default.urls
Here is a code sample to verify if a file named "database.sqlite" exists in application document directory:
这里有一个代码示例,用于验证一个名为“database”的文件。sqlite“存在于应用文档目录中:
func findIfSqliteDBExists(){
let docsDir : URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let dbPath : URL = docsDir.appendingPathComponent("database.sqlite")
let strDBPath : String = dbPath.path
let fileManager : FileManager = FileManager.default
if fileManager.fileExists(atPath:strDBPath){
print("An sqlite database exists at this path :: \(strDBPath)")
}else{
print("SQLite NOT Found at :: \(strDBPath)")
}
}
#7
2
Very simple: If your path is a URL instance convert to string by 'path' method.
非常简单:如果您的路径是一个URL实例,通过“path”方法将其转换为字符串。
let fileManager = FileManager.default
var isDir: ObjCBool = false
if fileManager.fileExists(atPath: yourURLPath.path, isDirectory: &isDir) {
if isDir.boolValue {
//it's a Directory path
}else{
//it's a File path
}
}
#8
0
You must add a "/" slash before filename, or you get path like ".../DocumentsFilename.jpg"
您必须在文件名之前添加“/”斜杠,否则您将获得“…/DocumentsFilename.jpg”之类的路径。