Create folder in Home directory

I am trying to create a folder in swift. The following code shows my folder creation

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)
}

      

Now I can create a folder in the document directory. But I need to create a folder on the path /Users/macuser/

. On this basis. Help is needed

+3


source to share


1 answer


I believe NSHomeDirectory()

this is what you are looking for.

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)
}

      



Or a little more succinctly:

let dataPath = "\(NSHomeDirectory())/MyFolder"

      

+7


source







All Articles