gpt4 book ai didi

ios - 有没有比使用 NSCoder 编码和解码所有内容更好的方法将自定义类保存到 NSUserDefaults?

转载 作者:搜寻专家 更新时间:2023-11-01 05:57:28 26 4
gpt4 key购买 nike

为了让我的类(class)与 NSUserDefaults 兼容,我当前的类(class)有大约 50 行代码和解码变量。有更好的方法来处理这个问题吗?

示例:

 init(coder aDecoder: NSCoder!) {
lightEnabled = aDecoder.decodeBoolForKey("lightEnabled")
soundEnabled = aDecoder.decodeBoolForKey("soundEnabled")
vibrateEnabled = aDecoder.decodeBoolForKey("vibrateEnabled")
pulseEnabled = aDecoder.decodeBoolForKey("pulseEnabled")
songs = aDecoder.decodeObjectForKey("songs") as! [Song]
currentSong = aDecoder.decodeIntegerForKey("currentSong")
enableBackgroundSound = aDecoder.decodeBoolForKey("enableBackgroundSound")
mixSound = aDecoder.decodeBoolForKey("mixSound")
playSoundInBackground = aDecoder.decodeBoolForKey("playSoundInBackground")
duckSounds = aDecoder.decodeBoolForKey("duckSounds")
BPMBackground = NSKeyedUnarchiver.unarchiveObjectWithData(aDecoder.decodeObjectForKey("BPMBackgorund") as! NSData) as! UIColor!
BPMPulseColor = NSKeyedUnarchiver.unarchiveObjectWithData(aDecoder.decodeObjectForKey("BPMPulseColor") as! NSData) as! UIColor!
TempoBackGround = NSKeyedUnarchiver.unarchiveObjectWithData(aDecoder.decodeObjectForKey("TempoBackGround") as! NSData) as! UIColor!
TempoPulseColor = NSKeyedUnarchiver.unarchiveObjectWithData(aDecoder.decodeObjectForKey("TempoPulseColor") as! NSData) as! UIColor!
TimeBackGround = NSKeyedUnarchiver.unarchiveObjectWithData(aDecoder.decodeObjectForKey("TimeBackGround") as! NSData) as! UIColor!
TimeStrokeColor = NSKeyedUnarchiver.unarchiveObjectWithData(aDecoder.decodeObjectForKey("TimeStrokeColor") as! NSData) as! UIColor!
TextColor = NSKeyedUnarchiver.unarchiveObjectWithData(aDecoder.decodeObjectForKey("TextColor") as! NSData) as! UIColor!
}

func encodeWithCoder(aCoder: NSCoder!) {
aCoder.encodeBool(lightEnabled, forKey: "lightEnabled")
aCoder.encodeBool(soundEnabled, forKey: "soundEnabled")
aCoder.encodeBool(vibrateEnabled, forKey: "vibrateEnabled")
aCoder.encodeBool(pulseEnabled, forKey: "pulseEnabled")
aCoder.encodeObject(songs, forKey: "songs")
aCoder.encodeInteger(currentSong, forKey: "currentSong")
aCoder.encodeBool(enableBackgroundSound, forKey: "enableBackgroundSound")
aCoder.encodeBool(mixSound, forKey: "mixSound")
aCoder.encodeBool(playSoundInBackground, forKey: "playSoundInBackground")
aCoder.encodeBool(duckSounds, forKey: "duckSounds")
aCoder.encodeObject(BPMBackground.archivedData(), forKey: "BPMBackground")
aCoder.encodeObject(BPMPulseColor.archivedData(), forKey: "BPMPulseColor")
aCoder.encodeObject(TempoBackGround.archivedData(), forKey: "TempoBackGround")
aCoder.encodeObject(TempoPulseColor.archivedData(), forKey: "TempoPulseColor")
aCoder.encodeObject(TimeBackGround.archivedData(), forKey: "TimeBackGround")
aCoder.encodeObject(TimeStrokeColor.archivedData(), forKey: "TimeStrokeColor")
aCoder.encodeObject(TextColor.archivedData(), forKey: "TextColor")
}

最佳答案

您应该创建一个结构或枚举来组织您的 key ,因为您的方式很容易出现拼写错误。把它放在你类(class)的正上方

enum Key: String {
case allSettings

case lightEnabled
case soundEnabled
}

不仅仅是像这样调用键

...forKey: Key.lightEnabled.rawValue)

关于您的问题,我在尝试保存 40 个关卡的属性(最佳时间、关卡解锁状态等)时遇到了同样的问题。我最初做了你尝试过的事情,这简直是疯狂。

我最终为我的数据使用了数组/字典甚至字典数组,这将我的代码减少了大约 80%。

还有一个好处是,如果您需要保存 LevelUnlock bools 之类的东西,它会让您以后的生活变得更加轻松。在我的例子中,我有一个 UnlockAllLevels 按钮,现在我可以在我的字典/数组中循环,并在几行代码中更新/检查 levelUnlock bools。比使用大量 if-else 或 switch 语句来单独检查每个属性要好得多。

例如

 var settingsDict = [
Key.lightEnabled.rawValue: false,
Key.soundEnabled.rawValue: false,
...
]

比在解码器方法中你这样说

注意:这种方式将考虑到您可能会向 SettingsDict 添加新值,并且在下一次应用启动时不会删除这些值,因为您没有用保存的字典替换整个字典,您只更新已经存在的值。

 // If no saved data found do nothing
if var savedSettingsDict = decoder.decodeObjectForKey(Key.allSettings.rawValue) as? [String: Bool] {
// Update the dictionary values with the previously saved values
savedSettingsDict.forEach {
// If the key does not exist anymore remove it from saved data.
guard settingsDict.keys.contains($0) else {
savedSettingsDict.removeValue(forKey: $0)
return
}
settingsDict[$0] = $1
}
}

如果你使用多个词典,那么你的解码器方法将再次变得困惑,你也会重复很多代码。为避免这种情况,您可以使用泛型创建 NSCoder 的扩展。

 extension NSCoder {

func decodeObject<T>(_ object: [String: T], forKey key: String) -> [String: T] {
guard var savedData = decodeObject(forKey: key) as? [String: T] else { return object }

var newData = object

savedData.forEach {
guard object.keys.contains($0) else {
savedData[$0] = nil
return
}

newData[$0] = $1
}

return newData
}
}

然后你可以在每个字典的解码器方法中写这个。

settingsDict = aDecoder.decodeObject(settingsDict, forKey: Key.allSettings.rawValue)

您的编码器方法如下所示。

 encoder.encodeObject(settingsDict, forKey: Key.allSettings.rawValue)

在您的游戏/应用中,您可以像这样使用它们

settingsDict[Key.lightEnabled.rawValue] = true

if settingsDict[Key.lightEnabled.rawValue] == true {
/// light is turned on, do something
}

这种方式也使得集成 iCloud KeyValue 存储变得非常容易(只需创建一个 iCloud 字典),同样主要是因为它非常容易保存和比较大量的值,只需很少的代码。

更新:

为了更轻松地调用它们,我喜欢在 GameData 类中创建一些方便的 getter/setter。这样做的好处是您可以更轻松地在项目中调用这些属性(就像您的旧方法一样),但您的编码/解码方法仍将保持紧凑。您还可以执行诸如循环比较值之类的操作。

 var isLightEnabled: Bool {
get { return settingsDict[Key.lightEnabled.rawValue] ?? false }
set { settingsDict[Key.lightEnabled.rawValue] = newValue }
}

var isSoundEnabled: Bool {
get { return settingsDict[Key.soundEnabled.rawValue] ?? false }
set { settingsDict[Key.soundEnabled.rawValue] = newValue }
}

而且您可以像普通属性一样调用它们。

isLightEnabled = true

if isLightEnabled {
/// light is turned on, do something
}

关于ios - 有没有比使用 NSCoder 编码和解码所有内容更好的方法将自定义类保存到 NSUserDefaults?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35821732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com