我一直在开发一个iPad绘图应用程序,它使用NSUserDefaults来存储包含用户调色板选项的NSDictionary对象数组。颜色使用整数存储,该整数表示应用加载时在数组中初始化的颜色的位置。存储在NSDictionary中的第二个元素是作为float的alpha值。该应用程序在设备和模拟器上运行良好 - 除非模拟器设置为iPad Retina(64位)。
错误是:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFNumber 0xb000000000000003> valueForUndefinedKey:]: this class is not key value coding-compliant for the key colorIndex.'
在AppDelegate中,调色板在实例化调色板变量的方法中初始化如下:
- (NSMutableArray *) palette
{
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
NSArray *userPalette = [userdefaults arrayForKey:@"palette"];
NSMutableArray *palette = [userPalette mutableCopy];
if (!palette) // get 12 initial colors for palette
{
palette = [[NSMutableArray alloc] initWithCapacity:12];
for (int x = 0; x < 12; x++) {
NSDictionary *paletteObject = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt: x], @"colorIndex",
[NSNumber numberWithFloat: 1.0], @"colorOpacity",
nil];
[palette addObject: paletteObject];
}
}
return palette;
}
首次启动时,会使用列表中的前12种颜色。
ViewController包含调色板,作为在故事板中设置的子类UIButtons的IBOutletCollection。
@property (nonatomic, strong) IBOutletCollection (ColorButton) NSArray *paletteButtons;
当ViewController在64位版本上启动时,以下代码会触发崩溃:
- (void) loadPaletteButtons
{
[[NSUserDefaults standardUserDefaults] synchronize];
_palette = [[[NSUserDefaults standardUserDefaults] objectForKey:@"palette"] mutableCopy];
NSLog(@"CP | loadPaletteButtons ");
for (int i = 0; i < [_palette count]; i++)
{
NSDictionary *paletteColor = _palette[i];
NSLog(@"[paletteColor valueForKey:@colorIndex is %@", [paletteColor valueForKey:@"colorIndex"] );
int colorIndex = [[paletteColor valueForKey:@"colorIndex"] intValue];
[_paletteButtons[i] setBackgroundColor:[_colors objectAtIndex: colorIndex]];
[_paletteButtons[i] setTag: colorIndex];
if (![paletteColor valueForKey: @"colorOpacity"]) {
[_paletteButtons[i] setColorOpacity: [[paletteColor valueForKey: @"colorOpacity"] floatValue]];
} else {
[_paletteButtons[i] setColorOpacity: 1.0];
}
} // end for loop to load palette buttons
}
该方法触发NSLog输出;也就是说,NSUserDefaults似乎没有触发错误。但colorIndex值似乎是个问题。我还没有能够在这个或任何其他网站上找到解释问题的任何内容 - 也不知道如何解决它。 我很难过。
堆栈追踪:
2014-06-02 12:48:12.955 theApp[871:60b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFNumber 0xb000000000000003> valueForUndefinedKey:]: this class is not key value coding-compliant for the key colorIndex.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000101e05495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000101b6499e objc_exception_throw + 43
2 CoreFoundation 0x0000000101e89919 -[NSException raise] + 9
3 Foundation 0x00000001017c93c6 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 226
4 Foundation 0x0000000101746155 -[NSObject(NSKeyValueCoding) valueForKey:] + 251
5 theApp 0x000000010000f602 -[ControlPanel loadPaletteButtons] + 402
6 theApp 0x000000010000f17c -[ControlPanel viewDidLoad] + 364
7 UIKit 0x000000010080759e -[UIViewController loadViewIfRequired] + 562
8 UIKit 0x0000000100807777 -[UIViewController view] + 29
9 theApp 0x00000001000151cc -[theAppViewController viewDidLoad] + 828
10 UIKit 0x000000010080759e -[UIViewController loadViewIfRequired] + 562
11 UIKit 0x0000000100807777 -[UIViewController view] + 29
12 UIKit 0x000000010074796b -[UIWindow addRootViewControllerViewIfPossible] + 58
13 UIKit 0x0000000100747c70 -[UIWindow _setHidden:forced:] + 282
14 UIKit 0x0000000100750ffa -[UIWindow makeKeyAndVisible] + 51
15 UIKit 0x000000010070cc98 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1788
16 UIKit 0x0000000100710a0c -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660
17 UIKit 0x0000000100721d4c -[UIApplication handleEvent:withNewEvent:] + 3189
18 UIKit 0x0000000100722216 -[UIApplication sendEvent:] + 79
19 UIKit 0x0000000100712086 _UIApplicationHandleEvent + 578
20 GraphicsServices 0x000000010346371a _PurpleEventCallback + 762
21 GraphicsServices 0x00000001034631e1 PurpleEventCallback + 35
22 CoreFoundation 0x0000000101d87679 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
23 CoreFoundation 0x0000000101d8744e __CFRunLoopDoSource1 + 478
24 CoreFoundation 0x0000000101db0903 __CFRunLoopRun + 1939
25 CoreFoundation 0x0000000101dafd83 CFRunLoopRunSpecific + 467
26 UIKit 0x00000001007102e1 -[UIApplication _run] + 609
27 UIKit 0x0000000100711e33 UIApplicationMain + 1010
28 theApp 0x00000001000015f3 main + 115
29 libdyld.dylib 0x0000000102bd25fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)