学习网址:https://github.com/potato512/SYSwiftLearning
效果图
代码源
// 保存,或修改 func saveInfo(name:String) { if (0 <= name.characters.count) { let userDefault = NSUserDefaults.standardUserDefaults() userDefault.setObject(name,forKey: "name") userDefault.synchronize() let alert = UIAlertView(title: "温馨提示",message: "保存成功",delegate: nil,cancelButtonTitle: "知道了") alert.show() } }
// 读取 func readInfo() -> String { let userDefault = NSUserDefaults.standardUserDefaults() let name = userDefault.objectForKey("name") as? String let alert = UIAlertView(title: "温馨提示",message: "读取成功:\(name)",cancelButtonTitle: "知道了") alert.show() if (name != nil) { return name! } return "" }
// 删除 func deleteInfo() { let useDefault = NSUserDefaults.standardUserDefaults() useDefault.removeObjectForKey("name") let alert = UIAlertView(title: "温馨提示",message: "删除成功",cancelButtonTitle: "知道了") alert.show() }原文链接:https://www.f2er.com/swift/322606.html