开发者

Objective-C save NSColor in NSUserDefaults

开发者 https://www.devze.com 2023-02-11 01:16 出处:网络
How do you save the color of the dot so that when the app is opened and closed the dot is the color it was last set to by the user?

How do you save the color of the dot so that when the app is opened and closed the dot is the color it was last set to by the user?

Could someone explain to me how to use NSUserDefaults and in which methods to declare NSUserDefaults.

So far i have this:

NSData *data = [NSArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"MyColor"];

N开发者_如何学编程SData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyColor"];
NSColor *color = [NSUnarchiver unarchiveObjectWithData:data];

Link for tutorial I followed: http://www.mactech.com/articles/mactech/Vol.25/25.04/2504RoadtoCode/index.html


This is what I use:

- (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];

    [BFChatWindow refresh];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消