How to create a file in Swift

October 15, 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

if(!fileManager.fileExists(atPath:newFile)){
   fileManager.createFile(atPath: newFile, contents: nil, attributes: nil)
}else{
    print("File is already created")
}