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

时间:2021-06-04 07:38:17

I have a litle problem with saving a color in NSUserDefaults . In objective-c is easy :

我在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];
}

The above code works fine in objective-c, but when i try to write same code in swift, I receive errors at reading color from NSUserDefaults.

上面的代码在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
}

Thanks , and sorry for my english !

谢谢,对不起我的英文!

2 个解决方案

#1


11  

For Objective C

对于Objective C

place like this.

像这样的地方。

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

To get back like this.

要像这样回来。

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

For Swift

set like this

像这样设置

 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")
}

get like this.

得到这样的。

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

#2


0  

Storing an NSColor instance in user defaults

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

store the value in aColor in user defaults as the value for key aKey

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

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

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

#1


11  

For Objective C

对于Objective C

place like this.

像这样的地方。

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

To get back like this.

要像这样回来。

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

For Swift

set like this

像这样设置

 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")
}

get like this.

得到这样的。

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

#2


0  

Storing an NSColor instance in user defaults

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

store the value in aColor in user defaults as the value for key aKey

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

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

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