ファイル名を作成:ドキュメントディレクトリのURLを検索
let fileName = "file"
を:
let DocumentDirURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
は絶対ファイルパスを作成するには、ファイル名と拡張子を追加します。
let fileURL = DocumentDirURL.URLByAppendingPathComponent(fileName).URLByAppendingPathExtension("csv")
NSFileHandleを使用してファイルを開きます。
ファイルの末尾にの
let file: NSFileHandle? = NSFileHandle(forUpdatingAtPath: fileURL.path!)
書き込みデータ:
if file == nil {
NSLog("File open failed")
} else {
// assuming data contains contents to be written
let fileData = data.dataUsingEncoding(NSUTF8StringEncoding)
// seek to end of the file to append at the end of the file.
file?.seekToEndOfFile()
file?.writeData(fileData!)
file?.closeFile()
}