Search code examples
iosswiftnsfilemanager

How to create a subdirectory using URLForDirectory?


This is what I have now:

let path = try! NSFileManager.defaultManager().URLForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomain: NSSearchPathDomainMask.UserDomainMask, appropriateForURL: nil, create: true)

this is the output for print(path):

file:///Users/kuna/Library/Developer/CoreSimulator/Devices/92BD140D-5C14-43C4-80D6-904BB9594ED6/data/Containers/Data/Application/CD9B3821-C536-4B46-B838-259490FD008E/Documents/

Inside that directory I need to create images subdirectory.

What code should I perform to achieve it?

The documentation says about apriopriateForURL:

The name of a directory inside of which you want to create a unique temporary directory for autosaving documents or some other use.


Solution

  • let path = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
    let imageURL = path?.URLByAppendingPathComponent("images")
    try! NSFileManager.defaultManager().createDirectoryAtURL(imageURL!, withIntermediateDirectories: true, attributes: nil)