"contentsOfFile" returns zero, a possible cause

Upon receipt of the contents of the csv file, nil is returned. However, reducing the csv table to 10 rows will work correctly, displaying the contents of the csv file.

The original csv has about 400,000 characters arranged in 500 rows and 11 columns. What can make it return null with the original csv?

let dbPath = "/Volumes/Gios2TWD/myDB.csv"

var error: NSError?

let csvContent = NSString(contentsOfFile: dbPath, encoding:NSUTF8StringEncoding, error: &error) as String!

println(csvContent)

println(error)

I am running Xcode Version 6.1 (6A1030)

error:

Optional (Error Domain = NSCocoaErrorDomain Code = 261 "File" myDB.csv "cannot be opened using Unicode text encoding (UTF-8)." UserInfo = 0x10050c5b0 {NSFilePath = / Volumes / Gios2TWD / myDB.csv, NSStringEc })

+4
source share
2 answers

, - :

let dbPath = "/Volumes/Gios2TWD/myDB.csv"
var error: NSError?
let csvContent = NSString(contentsOfFile: dbPath, encoding:NSUTF8StringEncoding, error: &error)
if csvContent != nil {
    // do something with the string
}
else {
    println("error: \(error)")
}

. SO, .

: " Unicode (UTF-8)", UTF-8. , - . NSMacOSRomanStringEncoding, 8- ASCII, . 8- ASCII.

. , 100% , .

+10

, Zaph NSMacOSRomanStringEnconding, - , NSUTF8StringEnconding.

, Swift, .

let csvContent: String?
do{
    csvContent = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
    // file could be read properly - do something with the content ...
} catch {
    let nsError = error as NSError
    print(nsError.localizedDescription)
}
+1

All Articles