0
我有處理大量違約一類,這是我如何初始化默認值:NSColor有時突變爲NSColorWell
+ (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];
}
但如果顏色裏面:方法我打印的顏色,有時它是類NSColorWell的,它創造的應用程序中的煩惱。