NSUserDefaults颜色错误(Swift)

时间:2016-08-28 21:53:55

标签: ios swift xcode nsuserdefaults uicolor

每当我保存一种颜色,如下所示:

slitherColor = sender.currentTitleColor
let userDefaults3 = NSUserDefaults.standardUserDefaults()
userDefaults3.setValue(slitherColor, forKey: "SSlitherC")
userDefaults3.synchronize()

我收到SIGABRT错误。当屏幕首次加载时,这就是我用来加载不存在错误的不存在的数据:

let userDefaults3 = NSUserDefaults.standardUserDefaults()
if let slitherColor2 = userDefaults.valueForKey("SSlitherC") {
    slitherColor = slitherColor2 as! UIColor
  }
    else {
    slitherColor = UIColor.yellowColor()    
  }

有关如何修复此错误的任何帮助?此外,这是SIGABRT错误消息:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object UIDeviceRGBColorSpace 0.0352708 1 0.161752 1 for key SSlitherC'

2 个答案:

答案 0 :(得分:0)

UIColors无法直接保存在NSUserDefaults中,因为NSUserDefaults基本上是一种美化的方式来访问您的应用程序的Plist文件。相反,您可以使用NSKeyedArchiver归档和取消归档颜色,以便可以正确保存。

将您的储蓄代码更改为:

slitherColor = sender.currentTitleColor
let userDefaults3 = NSUserDefaults.standardUserDefaults()
let slitherColorData = NSKeyedArchiver.archivedDataWithRootObject(slitherColor)
userDefaults3.setObject(slitherColorData, forKey: "SSlitherC")
userDefaults3.synchronize()

以及获取颜色的代码:

let userDefaults3 = NSUserDefaults.standardUserDefaults()
if let slitherColorData2 = userDefaults3.dataForKey("SSlitherC"), let slitherColor2 = NSKeyedUnarchiver.unarchiveObjectWithData(colorData) as? UIColor {
    slitherColor = slitherColor2
} else {
    slitherColor = UIColor.yellowColor()    
}

答案 1 :(得分:0)

您无法将UIColor直接保存到NSUserDefaults,为此,您需要存档和取消归档颜色对象并保存数据。 保存:

let colorData = NSKeyedArchiver.archivedDataWithRootObject(slitherColor)
userDefaults3.setObject(slitherColor, forKey: "SSlitherC")

并回来:

if let colorData = userDefaults3.dataForKey(key) {
  let color = NSKeyedUnarchiver.unarchiveObjectWithData(colorData) as? UIColor
}

Here是NSUserDefautls的扩展,有助于保存和检索数据。我将复制代码,以防gist文件被删除,我们仍然有有效的信息。

extension NSUserDefaults {

    func colorForKey(key: String) -> UIColor? {
        var color: UIColor?
        if let colorData = dataForKey(key) {
            color = NSKeyedUnarchiver.unarchiveObjectWithData(colorData) as? UIColor
        }
        return color
    }

    func setColor(color: UIColor?, forKey key: String) {
        var colorData: NSData?
        if let color = color {
            colorData = NSKeyedArchiver.archivedDataWithRootObject(color)
        }
        setObject(colorData, forKey: key)
    }

}

你可以调用这个方法

  1. 设置保存颜色userDefaults.setColor(color: color, forKey key: key)

  2. 获取颜色键userDefaults.colorForKey(key)

  3. Swift 3.0:

    扩展名UserDefaults {

    func color(forKey key: String) -> UIColor? {
        var color: UIColor?
        if let colorData = data(forKey: key) {
            color = NSKeyedUnarchiver.unarchiveObject(with: colorData) as? UIColor
        }
        return color
    }
    
    func set(color: UIColor?, forKey key: String) {
        var colorData: NSData?
        if let color = color {
            colorData = NSKeyedArchiver.archivedData(withRootObject: color)
        }
        set(colorData, forKey: key)
    }
    
    }
    

    <强>用法:

    let userDefault = UserDefaults.standard
    userDefault.set(color: color, forKey: key)
    userDefault.color(forKey: key)