[IOS] 使用属性列表实现数据持久化
2016-04-04 23:47
330 查看
// // ViewController.swift // just // // Created by Stary on 4/4/16. // Copyright © 2016 Stary. All rights reserved. // import UIKit class ViewController: UIViewController { // array of switches @IBOutlet var Switches : [UISwitch]! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let filePath = self.dataFilePath() // judge if the path is available if (NSFileManager.defaultManager().fileExistsAtPath(filePath)) { let array = NSArray(contentsOfFile: filePath) as! [Bool] for var i = 0; i < array.count; i++ { Switches[i].on = array[i] } } let app = UIApplication.sharedApplication() NSNotificationCenter.defaultCenter().addObserver(self, selector: "applicationWillResignActive:", name: UIApplicationWillResignActiveNotification, object: app) } // receive the message // Sent when the application is about to move from active to // interruptions (such as an incoming phone call or SMS message) or when the user quits the application // and it begins the transition to the background state. func applicationWillResignActive(notification:NSNotification) { let filePath = self.dataFilePath() let array = (self.Switches as NSArray).valueForKey("on") as! NSArray array.writeToFile(filePath, atomically: true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // 寻找文件路径 func dataFilePath() -> String { let paths = NSSearchPathForDirectoriesInDomains( NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) let documentsDirectory = paths[0] as NSString return documentsDirectory.stringByAppendingPathComponent("data.plist") as String } }
相关文章推荐
- IOS 简单动画 首尾式动画
- iOS开发者证书-详解/生成/使用,iOS CA认证
- nagios插件之监控udp端口状态
- iOS打电话、发短信
- iOS序列桢动画创建方式
- iOS开发中view controller设置问题
- ios AES、DES、MD5使用
- ios 添加到购物车的动画
- iOS开发-URLSession使用
- hadoop 集群部署ganglia 监控服务与nagios 报警服务
- IOS 创建简单表视图
- MapKit框架的使用
- Xcode免证书调试
- iOS设计模式之生成器
- iOS开发之通过cocoapods导入第三方类库
- iOS开发之瀑布流
- IOS- SDWebImage的作用
- ios 总结
- iOS 简单动画 序列帧动画
- iOS 简单动画 block动画