通用文件存储
import UIKit class ViewController: UIViewController { @IBOutlet weak var textField: UITextField! @IBAction func btnSave(sender: UIButton) { var text = textField.text as NSString //写入文件(可序列化) text.writeToFile(getPath("data.txt"), atomically: false, encoding: NSUTF8StringEncoding, error: nil) } @IBAction func btnLoad(sender: UIButton) { // 获取一个TXT的文件目录 var txtFilepath = getPath("data.txt")//字符串数据 if NSFileManager.defaultManager().fileExistsAtPath(txtFilepath) { var text = NSString(contentsOfFile: txtFilepath, encoding: NSUTF8StringEncoding, error: nil) textField.text = text as! String } } override func viewDidLoad() { super.viewDidLoad() /** 通用文件存储可以进行序列化的类(字典和数组) NSData NSString(String) NSNumber(Int, Double, Float) NSDate NSArray(Array) NSDictionary */ /** 自己创建的对象不能 没有在前面可序列化对象的其他类不能(UIColor,UIImage) 不能在自身里面 大数据一半不用 */ } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //获取文件目录 func getPath(fileName:String)->String { var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) var documents = paths[] as? String return documents!.stringByAppendingPathComponent(fileName) } }