Reputation: 789
I am experimenting with SwiftCSV to get data from a file, and so far I am having no luck. It seems like a very simple error that I cannot seem to fix - I am guessing it cannot find the file somehow. I have a "trainers.csv" file in the project (shown in screenshot) but every time I run this the code goes straight to the catch print statement - so there is something wrong in my try line.
func testCSV(){
print("I got here")
do {
let csv = try CSV(name: "trainers.csv")
print("Success?")
//print(csv.rows)
} catch {
print("it was in this moment Adrian knew, he messed up.")
}
}
Does anyone have any idea what the problem might be?
Upvotes: 1
Views: 591
Reputation: 6211
Their readme doesn't explain it very well, but looking into the CSV
class the constructor you are using takes in a URL as a parameter (in other words a full path). Then is uses that passed in path in String(contentsOfURL: url)
. So you need to get the full path to the file you are trying to load.
let csv = try CSV(name: NSBundle.mainBundle().pathForResource("trainers", ofType: "csv"))
Upvotes: 2