How to rename a folder in Swift

October 15, 2017

Use the moveItem() methof of the FileManager to rename a folder in iOS development

let home = FileManager.default.homeDirectoryForCurrentUser
let playgroundPath = "Documents/dev/xcode/DatePicker/c"

// get your playground URL
let playgroundURL = home.appendingPathComponent(playgroundPath)



// First create a FileManager instance
let fileManager = FileManager.default




let old = playgroundURL.appendingPathComponent("test-directory").path
let new = playgroundURL.appendingPathComponent("test-directory-new").path

do{
    try fileManager.moveItem(atPath: old, toPath: new)
} catch   {
    print("error")
}

Here I am renaming test-directory to test-directory-new