我正在尝试在 swift中创建一个文件夹.以下代码显示了我创建的文件夹 var error: NSError?var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, t
var error: NSError? var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) var documentsDirectory: AnyObject = paths[0] var dataPath = documentsDirectory.stringByAppendingPathComponent("MyFolder") if (!NSFileManager.defaultManager().fileExistsAtPath(dataPath)) { NSFileManager.defaultManager() .createDirectoryAtPath(dataPath, withIntermediateDirectories: false, attributes: nil, error: &error) }
现在我可以在文档目录下创建文件夹了.但是我需要在路径/ Users / macuser /中创建文件夹.在这一点上建造.需要帮忙
我相信NSHomeDirectory()
就是你要找的.
var error: NSError? var homeDirectory = NSHomeDirectory() var dataPath = homeDirectory.stringByAppendingPathComponent("MyFolder") if (!NSFileManager.defaultManager().fileExistsAtPath(dataPath)) { NSFileManager.defaultManager().createDirectoryAtPath(dataPath, withIntermediateDirectories: false, attributes: nil, error: &error) }
或者更简洁一点:
let dataPath = "\(NSHomeDirectory())/MyFolder"