Reputation: 341
Not sure why I am getting the error here. I created the folder 'Shared Playground Data' in documents as instructed. Does anyone have any insight?
import PlaygroundSupport
import Foundation
var fileName = "Csv.csv"
var csvText = "Date,Task,Time Started,Time Ended\n"
fileName.append(csvText)
let fileUrl = playgroundSharedDataDirectory.appendingPathComponent(fileName)
do {
try fileName.write(to: fileUrl, atomically: true, encoding: .utf8)
} catch {print("error")}
Upvotes: 2
Views: 2957
Reputation: 1173
I don't know which Xcode introduced this error, but playgroundSharedDataDirectory
directory can't be found by Playgrounds on Xcode 13 beta 2, so instead, do this:
let docDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileName = "Csv.csv"
let outputFileUrl = docDirectory.appendingPathComponent(fileName)
let csvText = "Date,Task,Time Started,Time Ended\n"
do {
try csvText.write(to: outputFileUrl, atomically: true, encoding: .utf8)
} catch {
print (error)
}
Upvotes: 2
Reputation: 318884
Your code makes no sense. Why do you append the column headings to the filename? And why do you write the contents of fileName
to fileUrl
?
You probably want the following:
let fileName = "Csv.csv"
let csvText = "Date,Task,Time Started,Time Ended\n"
let fileUrl = playgroundSharedDataDirectory.appendingPathComponent(fileName)
do {
try csvText.write(to: fileUrl, atomically: true, encoding: .utf8)
} catch {
print(error)
}
Upvotes: 6