Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a color in NSUserDefaults in swift?

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

-(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.

 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 !

like image 896
C-Viorel Avatar asked Oct 20 '14 11:10

C-Viorel


People also ask

How do you save Uicolor in Userdefaults?

The easiest way would be using NSUserDefaults . In this case you are storing the string "Coding Explorer" and you can reference by the key "userNameKey" .

How much data can you store in NSUserDefaults?

It appears the limit is the maximum file size for iOS (logically), which is currently 4GB: https://discussions.apple.com/thread/1763096?tstart=0. The precise size of the data is circumscribed by the compiler types (NSData, NSString, etc.) or the files in your asset bundle.

How do you save NSUserDefaults in Objective C?

Saving to NSUserDefaults : Basically, all you have to do is load NSUserDefaults, and then tell it to save a value to a specific key. Here is a simple example of writing an integer to NSUserDefaults: NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

What is NSUserDefaults in Swift?

Overview. The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an app to customize its behavior to match a user's preferences. For example, you can allow users to specify their preferred units of measurement or media playback speed.


1 Answers

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

For Swift 4.2

place like this.

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

To get back like this.

if let userSelectedColorData = UserDefaults.standard.object(forKey: "UserSelectedColor") as? Data {
    if let userSelectedColor = NSKeyedUnarchiver.unarchiveObject(with:userSelectedColorData as Data) as? UIColor {
        print(userSelectedColor)
    }
}
like image 68
Danial Hussain Avatar answered Sep 16 '22 21:09

Danial Hussain