iOS開發——資料持久化Swift篇&通用檔案儲存體

來源:互聯網
上載者:User

標籤:

通用檔案儲存體

 

 1 import UIKit 2  3 class ViewController: UIViewController { 4  5     @IBOutlet weak var textField: UITextField! 6      7     @IBAction func btnSave(sender: UIButton) { 8         var text = textField.text as NSString 9         10         //寫入檔案(可序列化)11         text.writeToFile(getPath("data.txt"), atomically: false, encoding: NSUTF8StringEncoding, error: nil)12         13     }14     15     @IBAction func btnLoad(sender: UIButton) {16         //        擷取一個TXT的檔案目錄17         var txtFilepath = getPath("data.txt")//字串資料18         if NSFileManager.defaultManager().fileExistsAtPath(txtFilepath) {19             var text = NSString(contentsOfFile: txtFilepath, encoding: NSUTF8StringEncoding, error: nil)20             textField.text = text as! String21         }22         23     }24     25     override func viewDidLoad() {26         super.viewDidLoad()27         /**28         通用檔案儲存體可以進行序列化的類(字典和數組)29             NSData30             NSString(String)31             NSNumber(Int, Double, Float)32             NSDate33             NSArray(Array)34             NSDictionary35         */36 /**    自己建立的對象不能37         沒有在前面可序列化對象的其他類不能(UIColor,UIImage)38         不能在自身裡面39         大資料一半不用40         */41 42         43     }44 45     override func didReceiveMemoryWarning() {46         super.didReceiveMemoryWarning()47         // Dispose of any resources that can be recreated.48     }49 //擷取檔案目錄50     func getPath(fileName:String)->String {51         var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)52         var documents = paths[0] as? String53         return documents!.stringByAppendingPathComponent(fileName)54     }55 56 }

 

 

iOS開發——資料持久化Swift篇&通用檔案儲存體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.