I have a problem with reading a file using swift. I have searched a lot on the web to se if i can find anything, I found 3 different, but I can't get any of them to work
here they are :
import Foundation
//----- 1 ------
var error: NSErrorPointer = nil
let url = NSURL(string: "ISO.csv")
let csvString = String(contentsOfURL:url!, encoding: NSUTF8StringEncoding, error: error);
println("contest")
println(csvString)
//----- 2 ------
func readFile(fileName: String, fileType: String) -> String{
var fileRoot = NSBundle.mainBundle().pathForResource(fileName, ofType: fileType)
var contents = NSString.stringWithContentsOfFile(fileRoot!, encoding: NSUTF8StringEncoding, error: nil)
return contents
}
// example: read file.txt
var contents = readFile("ISO", "csv")
println(contents)
//----- 3 -----
let path = NSBundle.mainBundle().pathForResource("ISO", ofType: "csv")
var data = String.stringWithContentsOfFile(path, encoding: NSUTF8StringEncoding, error: nil)
in number 1 : it just returns a nil. but it works with returning data from a website
number 2: /ISO/main.swift:26:20: 'String.Type' does not have a member named 'stringWithContentsOfFile'
number 3 : /ISO/main.swift:38:21: 'stringWithContentsOfFile(_:encoding:error:)' is unavailable: use object construction 'NSString(contentsOfFile:encoding:error:)'
i have no idea what to do, and hoped some of you knew, thanks in advance