问题
我是IOS开发的新手,并且一直在努力使用Core Data。我正在尝试使用开关创建设置页面。我需要记住这个开关是打开还是关闭。我创建了一个核心数据应用程序,并设法弄清楚如何将开关的值保存到属性。下面的代码执行此操作,我只是无法弄清楚如何将开关的保存值恢复为开或关。我该怎么做?谢谢!
核心数据图片
代码:
import UIKit
import CoreData
class preferencesStuff: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "Settings")
request.returnsObjectsAsFaults = false;
var results:NSArray = context.executeFetchRequest(request, error: nil)!
if(results.count > 0){
if results[results.count-1] as NSObject == 1 {
println("ON")
}
}else{
println("NO RESULTS")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var fractSwitchValue: UISwitch!
@IBAction func fractSwitch(sender: AnyObject) {
if fractSwitchValue.on == true {
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var newSetting = NSEntityDescription.insertNewObjectForEntityForName("Settings", inManagedObjectContext: context) as NSManagedObject
newSetting.setValue(true, forKey: "fractionOnOff")
context.save(nil)
//println(newSetting)
}
else {
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var newSetting = NSEntityDescription.insertNewObjectForEntityForName("Settings", inManagedObjectContext: context) as NSManagedObject
newSetting.setValue(false, forKey: "fractionOnOff")
context.save(nil)
//println(newSetting)
}
}
}
答案 0 :(得分:2)
我会在这种情况下使用NSUserDefaults。存储用户首选项时,它比Core Data更容易使用。例如,您将使用
存储该属性NSUserDefaults.standardUserDefaults().setBool(value, forKey: "fractionPreference")
并使用
检索它let value = NSUserDefaults.standardUserDefaults().boolForKey("fractionPreference")
答案 1 :(得分:2)
我刚刚用Core Data做了你的例子。我碰巧存储了NSString而不是Bool。没有优化,但它应该让你去。
import UIKit
import CoreData
class ViewController: UIViewController {
var state: NSString = ""
@IBOutlet weak var fractSwitchValue: UISwitch!
@IBAction func fractSwitch(sender: AnyObject) {
if fractSwitchValue.on == true {
state = "On"
save(state)
}
else {
state = "Off"
save(state)
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
var appDel = UIApplication.sharedApplication().delegate as AppDelegate
var context = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "Settings")
var error: NSError?
var results = context.executeFetchRequest(request, error: &error) as [NSManagedObject]?
if let fetchedResults = results {
state = fetchedResults[fetchedResults.count - 1].valueForKey("fractionOnOff") as String!
} else {
println("Could not fetch \(error), \(error!.userInfo)")
}
if state == "On" {
fractSwitchValue.setOn(true, animated: true)
} else {
fractSwitchValue.setOn(false, animated: true)
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
func save(string: NSString) {
let appDel = UIApplication.sharedApplication().delegate as AppDelegate
let context = appDel.managedObjectContext!
let entity = NSEntityDescription.entityForName("Settings", inManagedObjectContext: context)
let setting = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: context)
setting.setValue(string, forKey: "fractionOnOff")
println(string)
var error: NSError?
if !context.save(&error) {
println("Could not save \(error), \(error?.userInfo)")
}
}
}