How to write to temporary file in Swift

Issue #697

Use temporaryDirectory from FileManager and String.write

1
2
3
4
5
6
7
8
9
10
11
12
func writeTempFile(books: [Book]) -> URL {
let url = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString)
.appendingPathExtension("txt")
let string = books
.map({
"book '\($0.url.path)'"
})
.joined(separator: "\n")
try? string.write(to: url, atomically: true, encoding: .utf8)
return url
}

Comments