我有一个处理很多默认值的类,下面是我初始化默认值的方法:
+ (void) initialize
{
NSUserDefaults* defaults=[NSUserDefaults standardUserDefaults];
NSData* colorData= [NSKeyedArchiver archivedDataWithRootObject: [NSColor orangeColor]];
NSDictionary* def= @{ @"colorKey" : colorData, ... };
[defaults registerDefaults: def];
}然后,我提供了一个读取默认颜色的方法和一个设置默认颜色的方法:
- (NSColor*) color
{
NSUserDefaults* defaults=[NSUserDefaults standardUserDefaults];
NSData* colorData= [defaults objectForKey: colorKey];
return [NSKeyedUnarchiver unarchiveObjectWithData: colorData];
}
- (void) setColor: (NSColor*) color
{
NSUserDefaults* defaults=[NSUserDefaults standardUserDefaults];
NSData* colorData= [NSKeyedArchiver archivedDataWithRootObject: color];
[defaults setObject:colorData forKey: colorKey];
}但是如果在color:方法中打印颜色,有时它是NSColorWell类的,它会在应用程序内部造成麻烦。
发布于 2012-12-20 01:53:13
根据https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSColorWell_Class/Reference/Reference.html的说法,NSColorWell是一个NSControl,所以看起来您错误地将setColor:调用为NSColorWell实例,而不是NSColor。
https://stackoverflow.com/questions/13957948
复制相似问题