You'll have to handle NSColor in a special way, when storing it in user defaults. NSColor is not an object that can be represented in an property list; see Storing NSColor in User Defaults for code examples.
Xcode User Defaults unrecognized selector error
-
13-01-2022 - |
Question
I'm getting started to use User Defaults on Cocoa and I'm getting an [NSConcreteData alphaComponent]: unrecognized selector sent to instance 0x100123660
error. Here is the source code :
AppDelegate.m file :
- (void)awakeFromNib{
NSColor *myColor = [NSColor greenColor];
[PreferencesController setDefaultColor:myColor];
NSColor *color = (NSColor *)[PreferencesController defaultColor];
NSLog(@"Default color : %@ - %@",(NSColor *)color, [NSColor greenColor]);
[tableView setBackgroundColor:color];
[colorWell setColor:color];
}
PreferencesController.m file :
+ (NSColor *)defaultColor{
NSColor * aColor;
NSData *theData=[[NSUserDefaults standardUserDefaults] valueForKey:TableViewBackgroundColorKey];
if (theData != nil)
aColor = (NSColor *)[NSUnarchiver unarchiveObjectWithData:theData];
return aColor;
}
+ (void)setDefaultColor:(NSColor *)color{
NSData *theData=[NSArchiver archivedDataWithRootObject:[NSArchiver archivedDataWithRootObject:color]];
[[NSUserDefaults standardUserDefaults] setObject:theData forKey:TableViewBackgroundColorKey];
}
The return value of the NSLog is :
<040b7374 7265616d 74797065 6481e803 84014084 8484074e 53436f6c 6f720084 84084e53
4f626a65 63740085 84016301 84046666 66660001 000186> - NSCalibratedRGBColorSpace 0 1 0 1
So the value returned by + (NSColor *)defaultColor
is not an NSColor, and certainly not [NSColor greeenColor]
.
I'm also getting the error -[NSConcreteData alphaComponent]: unrecognized selector sent to instance 0x100184050
, which I think must be caused by not returning an NSColor
for the method mentioned.
Please help me to debug.
Thanks.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow