Trying to write data from UI to IOS File Storage :
@IBAction func savetoFile(sender: AnyObject) {
let csvLine:String="\(bookName.text), \(bookAuthor.text),\(bookIsbn.text)\n"
let paths = NSSearchPathForDirectoriesInDomains(
NSSearchPathDirectory.DocumentDirectory,
NSSearchPathDomainMask.UserDomainMask, true)
let docDir:String=paths[0] as String
let bookFile:String=(docDir as NSString).stringByAppendingPathComponent("bookresults.dat")
if !NSFileManager.defaultManager().fileExistsAtPath(bookFile) {
NSFileManager.defaultManager().createFileAtPath(bookFile,
contents: nil, attributes: nil)
}
let fileHandle:NSFileHandle=NSFileHandle(forUpdatingAtPath:bookFile)!
fileHandle.seekToEndOfFile()
fileHandle.writeData(csvLine.dataUsingEncoding(NSUTF8StringEncoding)!)
fileHandle.closeFile()
Problem :
If I give "BookOne" , program writes to file as "Optional("BookOne") and repeats this "Optional" for every field , every row.
Appreciate if someone could share the fix with code for this problem.
Tech stack is Xcode 7.1 and SWIFT.
Thanks