11

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
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Josh Schlabach
  • 401
  • 5
  • 17

2 Answers2

23

Swift 5.2 or later

Note that this will save only the RGBA CGFloat values as Data inside the property list. This will use 32 bytes (raw data) instead of 424 bytes needed when using the standard approach with NSKeyedUnarchiver (NSCoding):

extension Numeric {
    var data: Data {
        var bytes = self
        return Data(bytes: &bytes, count: MemoryLayout<Self>.size)
    }
}

extension Data {
    func object<T>() -> T { withUnsafeBytes{$0.load(as: T.self)} }
    var color: UIColor { .init(data: self) }
}

extension UIColor {
    convenience init(data: Data) {
        let size = MemoryLayout<CGFloat>.size
        self.init(red:   data.subdata(in: size*0..<size*1).object(),
                  green: data.subdata(in: size*1..<size*2).object(),
                  blue:  data.subdata(in: size*2..<size*3).object(),
                  alpha: data.subdata(in: size*3..<size*4).object())
    }
    var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)? {
        var (red, green, blue, alpha): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0)
        return getRed(&red, green: &green, blue: &blue, alpha: &alpha) ?
        (red, green, blue, alpha) : nil
    }
    var data: Data? {
        guard let rgba = rgba else { return nil }
        return rgba.red.data + rgba.green.data + rgba.blue.data + rgba.alpha.data
    }
}

extension UserDefaults {
    func set(_ color: UIColor?, forKey defaultName: String) {
        guard let data = color?.data else {
            removeObject(forKey: defaultName)
            return
        }
        set(data, forKey: defaultName)
    }
    func color(forKey defaultName: String) -> UIColor? {
        data(forKey: defaultName)?.color
    }
}

extension UserDefaults {
    var backgroundColor: UIColor? {
        get { color(forKey: "backgroundColor") }
        set { set(newValue, forKey: "backgroundColor") }
    }
}

UserDefaults.standard.backgroundColor = .red
UserDefaults.standard.backgroundColor  // r 1.0 g 0.0 b 0.0 a 1.0
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
-4

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

Community
  • 1
  • 1
jshbrmn
  • 1,737
  • 3
  • 22
  • 52
  • 1
    I've already looked at those websites. I'm trying to save the background color, and I need help doing so. I've looked for help and there is no help out there. – Josh Schlabach Dec 19 '15 at 01:17