How to create a file in Swift

Last Updated: October 16, 2017

Using the following code you can create new file in XCODE playground

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
// if file is not exist create the file

let newFile = playgroundURL.appendingPathComponent("test.txt").path

   fileManager.createFile(atPath: newFile, contents: nil, attributes: nil)
    print("File is already created")