ios 在 Swift 3.0 中创建目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41162610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create Directory in Swift 3.0
提问by Neeld
I am a new student in 9th grade learning swift, creating a school project .
我是 9 年级的新生,学习swift,正在创建一个学校项目。
I am trying to create a directory where I want to save a scanned file into pdf format.
我正在尝试创建一个目录,我想在其中将扫描的文件保存为 pdf 格式。
While creating directory I am getting error below.
在创建目录时,我收到以下错误。
Error 1:
错误 1:
Cannot use instance member 'filemgr' within property initializer; property initializers run before 'self' is available.
不能在属性初始值设定项中使用实例成员“filemgr”;属性初始值设定项在 'self' 可用之前运行。
Error 2:
错误 2:
Expected declaration
预期申报
Code:
代码:
let filemgr = FileManager.default
let dirPaths = filemgr.urls(for: .documentDirectory, in: .userDomainMask)
let docsURL = dirPaths[0]
let newDir = docsURL.appendingPathComponent("data").path
do{
try filemgr.createDirectory(atPath: newDir,withIntermediateDirectories: true, attributes: nil)
} catch {
print("Error: \(error.localizedDescription)")
}
Please assist me in resolving this issue.
请帮助我解决这个问题。
Thanks.
谢谢。
回答by Avinash
For Swift 4.0Please use this
对于Swift 4.0请使用这个
let fileManager = FileManager.default
if let tDocumentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first {
let filePath = tDocumentDirectory.appendingPathComponent("\(FOLDER_NAME)")
if !fileManager.fileExists(atPath: filePath.path) {
do {
try fileManager.createDirectory(atPath: filePath.path, withIntermediateDirectories: true, attributes: nil)
} catch {
NSLog("Couldn't create document directory")
}
}
NSLog("Document directory is \(filePath)")
}
回答by Pragnesh Vitthani
Pls, use this code:
请使用此代码:
Swift 4.0And Swift 3.0
Swift 4.0和 Swift 3.0
let DocumentDirectory = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
let DirPath = DocumentDirectory.appendingPathComponent("FOLDER_NAME")
do
{
try FileManager.default.createDirectory(atPath: DirPath!.path, withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError
{
print("Unable to create directory \(error.debugDescription)")
}
print("Dir Path = \(DirPath!)")
回答by CodeBender
For Swift 4.0, I created the following extension off of URLthat allows for the creation of a folder off of the documents directory within the application.
对于Swift 4.0,我创建了以下扩展URL,允许在应用程序中的文档目录之外创建文件夹。
import Foundation
extension URL {
static func createFolder(folderName: String) -> URL? {
let fileManager = FileManager.default
// Get document directory for device, this should succeed
if let documentDirectory = fileManager.urls(for: .documentDirectory,
in: .userDomainMask).first {
// Construct a URL with desired folder name
let folderURL = documentDirectory.appendingPathComponent(folderName)
// If folder URL does not exist, create it
if !fileManager.fileExists(atPath: folderURL.path) {
do {
// Attempt to create folder
try fileManager.createDirectory(atPath: folderURL.path,
withIntermediateDirectories: true,
attributes: nil)
} catch {
// Creation failed. Print error & return nil
print(error.localizedDescription)
return nil
}
}
// Folder either exists, or was created. Return URL
return folderURL
}
// Will only be called if document directory not found
return nil
}
}
If the desired folder does not exist, it will create it. Then, assuming the folder exists, it returns the URLback to the user. Otherwise, if it fails, then nilis returned.
如果所需的文件夹不存在,它将创建它。然后,假设文件夹存在,它将返回URL给用户。否则,如果失败,则nil返回。
For example, to create the folder "MyStuff", you would call it like this:
例如,要创建文件夹“MyStuff”,您可以这样称呼它:
let myStuffURL = URL.createFolder(folderName: "MyStuff")
This would return:
这将返回:
file:///var/mobile/Containers/Data/Application/4DE0A1C0-8629-47C9-87D7-C2B4F3A16D24/Documents/MyStuff/
file:///var/mobile/Containers/Data/Application/4DE0A1C0-8629-47C9-87D7-C2B4F3A16D24/Documents/MyStuff/
You can also create nested folders with the following:
您还可以使用以下内容创建嵌套文件夹:
let myStuffHereURL = URL.createFolder(folderName: "My/Stuff/Here")
Which gives you:
这给了你:
file:///var/mobile/Containers/Data/Application/4DE0A1C0-8629-47C9-87D7-C2B4F3A16D24/Documents/My/Stuff/Here/
file:///var/mobile/Containers/Data/Application/4DE0A1C0-8629-47C9-87D7-C2B4F3A16D24/Documents/My/Stuff/Here/


