這次來看看 UIColor。
如果你這樣用:
NSDictionary *dict0 = [[NSDictionary alloc] initWithObjectsAndKeys:
@"紅色", @"CColor",
[UIColor redColor], @"EColor",
@"YES", @"Fill",
nil];
NSDictionary *dict0 = [[NSDictionary alloc] initWithObjectsAndKeys:
@"紅色", @"CColor",
[UIColor redColor], @"EColor",
@"YES", @"Fill",
nil];
Xcode 會跟你這樣說:
-[UICachedDeviceRGBColor bytes]: unrecognized selector sent to instance 0xa9671e0
ColorInDictionary[7955:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICachedDeviceRGBColor bytes]: unrecognized selector sent to instance 0xa9671e0'
原因是 NSDictionary 內容不允許你直接存入 [UIColor redColor] 的關係。
於是你要將 [UIColor redColor] 包裝起來,改用 [NSKeyedArchiver archivedDataWithRootObject:[UIColor redColor]]
像這樣:
NSDictionary *dict0 = [[NSDictionary alloc] initWithObjectsAndKeys:
@"紅色", @"CColor",
[NSKeyedArchiver archivedDataWithRootObject:[UIColor redColor]], @"EColor",
@"YES", @"Fill",
nil];
像這樣:
NSDictionary *dict0 = [[NSDictionary alloc] initWithObjectsAndKeys:
@"紅色", @"CColor",
[NSKeyedArchiver archivedDataWithRootObject:[UIColor redColor]], @"EColor",
@"YES", @"Fill",
nil];
取用時再拆開,還原成 UIColor 即可
UIColor *colorNow = [NSKeyedUnarchiver unarchiveObjectWithData:[dict0 objectForKey:@"EColor"]];
UIColor *colorNow = [NSKeyedUnarchiver unarchiveObjectWithData:[dict0 objectForKey:@"EColor"]];
.
沒有留言:
張貼留言