let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "ja_JP")
dateFormatter.timeZone = NSTimeZone.systemTimeZone()
dateFormatter.dateFormat = "yyyy-MM-dd_HH-mm-ss"
dateFormatter.stringFromDate(NSDate()) # 2016-01-01_00-00-00 のように文字列がとれる
保存したファイルは、iTunesで端末を選択してAppのところから見れる。
let text = "hogehoge\n"
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!
let filePath = documentDirectory + "/" + "filename.dat"
do {
try text.writeToFile(filePath, atomically: false, encoding: NSUTF8StringEncoding)
} catch _ {
print("ファイル書き込み中に例外が発生しました")
}
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!
let manager = NSFileManager()
do {
let files = try manager.contentsOfDirectoryAtPath(documentDirectory)
for file_name in files {
print(file_name)
}
} catch _ {
}
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!
let manager = NSFileManager()
do {
let files = try manager.contentsOfDirectoryAtPath(documentDirectory)
for file_name in files {
try manager.removeItemAtPath(documentDirectory + "/" + file_name)
}
} catch _ {
}
UIAlertViewControllerを使う
let saveAlert: UIAlertController = UIAlertController(title: "Save", message: "Do you want to save it?", preferredStyle: .Alert)
let yesAction: UIAlertAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { (action:UIAlertAction!) -> Void in
print("Yes Clicked!")
})
let noAction: UIAlertAction = UIAlertAction(title: "No", style: UIAlertActionStyle.Default, handler: { (action:UIAlertAction!) -> Void in
print("No Clicked!")
})
saveAlert.addAction(noAction)
saveAlert.addAction(yesAction)
presentViewController(saveAlert, animated: true, completion: nil)