开发者

NSArrayController bound to NSUserDefaults with NSKeyedUnarchiveFromData value transformer

开发者 https://www.devze.com 2023-03-19 00:28 出处:网络
I have an NSArrayController instance bound to Shared User Defaults Controller. (see screenshot below) The key path points to an instance of NSData, when unarchived should give an array. But when the n

I have an NSArrayController instance bound to Shared User Defaults Controller. (see screenshot below) The key path points to an instance of NSData, when unarchived should give an array. But when the nib starts to load, it gives -

2011-07-10 23:55:23.093 MyApp[18139:507] -[__NSCFConstantString characterAtIndex:]: Range or index out of bounds
2011-07-10 23:55:23.106 MyApp[18139:507] (
    0   CoreFoundation                      0x00007fff95729986 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff90d5dd5e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff957297ba +[NSException raise:format:arguments:] + 106
    3   CoreFoundation                      0x00007fff95729744 +[NSException raise:format:] + 116
    4   CoreFoundation                      0x00007fff956e750e -[__NSCFString characterAtIndex:] + 94
    5   Foundation                          0x00007fff94c090a7 NSKeyValuePropertyForIsaAndKeyPathInner + 109
    6   Foundation                          0x00007fff94c08d43 NSKeyValuePropertyForIsaAndKeyPath + 159
    7   Foundation                          0x00007fff94c08afb -[NSObject(NSKeyValueObserverRegistration) addObserver:forKeyPath:options:context:] + 82
    8   Foundation                          0x00007fff94c3f5a0 -[NSKeyValueNestedProperty object:didAddObservance:recurse:] + 222
    9   Foundation                          0x00007fff94c0a177 -[NSObject(NSKeyValueObserverRegistration) _addObserver:forProperty:options:context:] + 481
    10  Foundation                          0x00007fff94c08b18 -[NSObject(NSKeyValueObserverRegistration) addObserver:forKeyPath:options:context:] + 111
    11  AppKit                              0x00007fff95beddd1 -[NSAutounbinder addObserver:forKeyPath:options:context:] + 210
    12  AppKit                              0x00007fff95a1e323 -[NSBinder _updateObservingRegistration:] + 859
    13  AppKit                              0x00007fff95a1d4a1 -[NSBinder establishConnection] + 314
    14  AppKit                              0x00007fff95a1185b -[NSObject(NSKeyValueBindingCreation) bind:toObject:withKeyPath:options:] + 591
    15  AppKit                              0x00007fff95a0ab81 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1079
    16  AppKit                              0x00007fff95a011b9 loadNib + 322
    17  AppKit                              0x00007fff95a006b6 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 217
    18  AppKit 开发者_C百科                             0x00007fff95a005d1 +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] + 141
    19  AppKit                              0x00007fff95bee328 -[NSWindowController loadWindow] + 221
    20  AppKit                              0x00007fff95bee0e3 -[NSWindowController window] + 75
    21  AppKit                              0x00007fff95bedf51 -[NSWindowController showWindow:] + 40
    22  MyApp                               0x00000001000024af -[MyAppDelegate showPreferencesWindow:] + 255
    23  MyApp                               0x0000000100005229 -[TBStatusWindowController preferences:] + 89
    24  CoreFoundation                      0x00007fff9571911d -[NSObject performSelector:withObject:] + 61
    25  AppKit                              0x00007fff95afd852 -[NSApplication sendAction:to:from:] + 139
    26  AppKit                              0x00007fff95afd784 -[NSControl sendAction:to:] + 88
    27  AppKit                              0x00007fff95afd6af -[NSCell _sendActionFrom:] + 137
    28  AppKit                              0x00007fff95afcb7a -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2014
    29  AppKit                              0x00007fff95b7c57c -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 489
    30  AppKit                              0x00007fff95afb786 -[NSControl mouseDown:] + 786
    31  AppKit                              0x00007fff95ac666e -[NSWindow sendEvent:] + 6280
    32  AppKit                              0x00007fff95a5ef19 -[NSApplication sendEvent:] + 5665
    33  AppKit                              0x00007fff959f542b -[NSApplication run] + 548
    34  AppKit                              0x00007fff95c7352a NSApplicationMain + 867
    35  MyApp                               0x0000000100001a40 main + 32
    36  MyApp                               0x0000000100001a14 start + 52
)

So what is the problem with my configuration?

NSArrayController bound to NSUserDefaults with NSKeyedUnarchiveFromData value transformer

NSArrayController bound to NSUserDefaults with NSKeyedUnarchiveFromData value transformer


Why should an empty NSData object deserialize to an empty NSArray? There's no data, so the unarchiver can't even know which class your object has. If you want to unarchive an empty NSArray, you have to archive an empty NSArray first. This will not result in an empty NSData.


The reason for the problem is totally unrelated to this one. It turns out that there is nothing wrong with my configuration.

Thanks for your time, omz.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号