mardi 19 juillet 2016

NSFileHandle updateatPath how can i update file instead of overwriting?

I am trying to create a file and update the file by adding a string to the end each time i hit save button. However, everytime i hit save it creates a new file (overwrites) with the latest string. My code is below, how can i fix it? please help! func pathToDocsFolder() -> String { let pathToDocumentsFolder =NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString return pathToDocumentsFolder.stringByAppendingPathComponent("/log.csv") } @IBAction func saveSample(sender: AnyObject) { let string = LabelText + ";" + SampleNoOne.text! + ";" + fromOne.text! + ";" + toOne.text! + ";" + casingBlowsOne.text! + ";" let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)! let filemanager = NSFileManager.defaultManager() if filemanager.fileExistsAtPath(pathToDocsFolder()){ filemanager.createFileAtPath(pathToDocsFolder(), contents: data, attributes: nil) } let fileHandle = NSFileHandle.init(forUpdatingAtPath: pathToDocsFolder()) fileHandle!.seekToEndOfFile() fileHandle!.writeData(data) fileHandle!.closeFile()

Aucun commentaire:

Enregistrer un commentaire