This question already has an answer here:
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
}
Swift 4.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 UserDefaults {
func set(_ color: UIColor?, forKey defaultName: String) {
var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
guard let color = color, color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
else {
removeObject(forKey: defaultName)
return
}
let count = MemoryLayout<CGFloat>.size
set(Data(bytes: &red, count: count) +
Data(bytes: &green, count: count) +
Data(bytes: &blue, count: count) +
Data(bytes: &alpha, count: count), forKey: defaultName)
}
func color(forKey defaultName: String) -> UIColor? {
guard let data = data(forKey: defaultName) else {
return nil
}
let size = MemoryLayout<CGFloat>.size
return UIColor(red: data[size*0..<size*1].withUnsafeBytes{ $0.pointee },
green: data[size*1..<size*2].withUnsafeBytes{ $0.pointee },
blue: data[size*2..<size*3].withUnsafeBytes{ $0.pointee },
alpha: data[size*3..<size*4].withUnsafeBytes{ $0.pointee })
}
}
extension UserDefaults {
var backgroundColor: UIColor? {
get {
return 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
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
来源:https://stackoverflow.com/questions/34366171/how-do-i-save-a-uicolor-with-userdefaults