How do I save a UIColor with NSUserDefaults? [dupl

2019-01-27 19:10发布

问题:

This question already has an answer here:

  • Saving UIColor to and loading from NSUserDefaults 7 answers

I'm trying to program my code so that if the user presses the Night Button the background will turn black and stay black if the user closes the app. (Same goes for day mode.)

Please note: I already coded buttons and when they press it, all of the scenes change to that mode.

Here's my code where I'm going going to need the background color to be saved: (I need it in both if statements)

if GlobalData.dayBool == true && GlobalData.night == false {
    backgroundColor = GlobalData.dayColor 
}

if GlobalData.nightBool == true && GlobalData.dayBool == false {
    backgroundColor = GlobalData.nightColor 
}

My Night and Day Colors:

struct GlobalData {
    static var score = 0
    static var dayColor = UIColor(red:0.93, green:0.93, blue:0.93, alpha:1.0)
    static var nightColor = UIColor(red:0.10, green:0.10, blue:0.10, alpha:1.0)  
    static var dayBool = true
    static var nightBool = true
}

回答1:

extension NSUserDefaults {
    func setColor(value: UIColor?, forKey: String) {
        guard let value = value else {
            setObject(nil, forKey:  forKey)
            return
        }
        setObject(NSKeyedArchiver.archivedDataWithRootObject(value), forKey: forKey)
    }
    func colorForKey(key:String) -> UIColor? {
        guard let data = dataForKey(key), color = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? UIColor
        else { return nil }
        return color
    }
}

You can also create a UIColor with a getter setter to save it automatically as follow:

var nightColor: UIColor? {
    get {
        return NSUserDefaults.standardUserDefaults().colorForKey("nightColor")
    }
    set {
        NSUserDefaults.standardUserDefaults().setColor(newValue, forKey: "nightColor")
    }
}

edit/update:

Xcode 8.1 • Swift 3.0.1

extension UserDefaults {
    func set(_ color: UIColor, forKey key: String) {
        set(NSKeyedArchiver.archivedData(withRootObject: color), forKey: key)
    }
    func color(forKey key: String) -> UIColor? {
        guard let data = data(forKey: key) else { return nil }
        return NSKeyedUnarchiver.unarchiveObject(with: data) as? UIColor
    }
}

var nightColor: UIColor? {
    get {
        return UserDefaults.standard.color(forKey: "nightColor")
    }
    set {
        UserDefaults.standard.set(newValue, forKey: "nightColor")
    }
}


回答2:

The easiest way would be using NSUserDefaults.

Simple example -

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject("Coding Explorer", forKey: "userNameKey")

In this case you are storing the string "Coding Explorer" and you can reference by the key "userNameKey". I'm sure you can see how you can work this into your solution :P

A good source to help you get started here

If you want something a little more robust, take a look at CoreData, the implementation is a little more complicated however. CoreData may not be what you need for simple state persistance but may be worth looking into nonetheless.

Link here

Edit

Here is a great example to look at how you can save the actual color to NSUserData