如何在swift中保存NSUserDefaults中的颜色?

问题描述 投票:6回答:2

我在NSUserDefaults中保存颜色有一个小问题。在objective-c很容易:

-(NSColor *)colorForKey:(NSString *)key{
NSData  *data;
NSColor *color;
data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
color= [NSUnarchiver unarchiveObjectWithData:data];
if( ! [color isKindOfClass:[NSColor class]] ){
    color = nil;
}
return color;
}

-(void)setColor:(NSColor *)color forKey:(NSString *)key{
    NSData *data = [NSArchiver archivedDataWithRootObject:color];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:key];
}

上面的代码在objective-c中工作正常,但是当我尝试在swift中编写相同的代码时,我在从NSUserDefaults读取颜色时收到错误。

 func saveColorForKey (color:NSColor , key:NSString ) {

    var data:NSData = NSData()
    data = NSArchiver.archivedDataWithRootObject(color)
    NSUserDefaults.standardUserDefaults().setObject(data, forKey: key)
}
func colorForKey(key:NSString) -> NSColor{
    var  data: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey(key)
    var color: AnyObject? =  NSUnarchiver.unarchiveObjectWithData(data? as NSData)

    return color as NSColor
}

谢谢,对不起我的英文!

objective-c swift nsuserdefaults osx-yosemite
2个回答
12
投票

对于Objective C

像这样的地方。

NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"myColor"];

要像这样回来。

NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];

对于斯威夫特

像这样设置

 var userSelectedColor : NSData? = (NSUserDefaults.standardUserDefaults().objectForKey("UserSelectedColor") as? NSData)

if (userSelectedColor != nil) {
    var colorToSetAsDefault : UIColor = UIColor.redColor()
    var data : NSData = NSKeyedArchiver.archivedDataWithRootObject(colorToSetAsDefault)
    NSUserDefaults.standardUserDefaults().setObject(data, forKey: "UserSelectedColor")
    NSUserDefaults.standardUserDefaults().synchronize()
    println("SET DEFAULT USER COLOR TO RED")
}

得到这样的。

if let userSelectedColorData = NSUserDefaults.standardUserDefaults().objectForKey("UserSelectedColor") as? NSData {
    if let userSelectedColor = NSKeyedUnarchiver.unarchiveObjectWithData(userSelectedColorData) as? UIColor {
        println(userSelectedColor)
    }
}

对于Swift 4.2

像这样的地方。

let colorToSetAsDefault : UIColor = UIColor.red
let data : Data = NSKeyedArchiver.archivedData(withRootObject: colorToSetAsDefault) as Data
UserDefaults.standard.set(data, forKey: "UserSelectedColor")
UserDefaults.standard.synchronize()
print("SET DEFAULT USER COLOR TO RED")

要像这样回来。

if let userSelectedColorData = UserDefaults.standard.object(forKey: "UserSelectedColor") as? Data {
    if let userSelectedColor = NSKeyedUnarchiver.unarchiveObject(with:userSelectedColorData as Data) as? UIColor {
        print(userSelectedColor)
    }
}

0
投票

在用户默认值中存储NSColor实例

将用户默认值中的值存储在aColor中作为键aKey的值

NSData * theData = [NSArchiver archivedDataWithRootObject:aColor]; [[NSUserDefaults standardUserDefaults] setObject:theData forKey:aKey];

© www.soinside.com 2019 - 2024. All rights reserved.