gpt4 book ai didi

objective-c - [NSKeyedUnarchiver unarchiveObjectWithData :]; 上的 EXC_BAD_ACCESS

转载 作者:搜寻专家 更新时间:2023-10-30 19:52:07 24 4
gpt4 key购买 nike

设置赏金后我找到了答案。如果有人对我的方法有一些有用的评论(我不知道这是否是“正确”的解决方案),他/她可以获得赏金。如果您要对我的问题投反对票,请发表评论,以便我改进 future 的问题。

我目前正在通过 Cocoa Programming For Mac OS X 一书学习 Objective-C,它在第 10 章介绍了归档。我(AFAIK)完全按照作者的要求做了,但是打开文件并取消存档时,应用程序崩溃了:

array = [NSKeyedUnarchiver unarchiveObjectWithData:data];

它说 GDB 收到信号:EXC_BAD_ACCESS。我只在访问越界的数组槽时遇到过这种情况,我相信不会那样做。我最好的猜测是 Cocoa 幕后出现问题,这是由我间接造成的。这会是什么?
正如我所说,我目前正在学习 Objective-C(但我知道 Java),所以不要指望我知道每一个晦涩的语言特性。

文件打开方式(MyDocument.m):

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
NSMutableArray *array = nil;
NSLog(@"data is %@", data);

@try {
array = [NSKeyedUnarchiver unarchiveObjectWithData:data]; // line of the EXC_BAD_ACCESS
}
@catch (NSException * e) {
if (outError) {
NSDictionary *d = [NSDictionary dictionaryWithObject:@"The data is corrupted." forKey:NSLocalizedFailureReasonErrorKey];
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:d];
}
return NO;
}
[self setEmployees:array];
return YES;
}

文件保存方法(还是MyDocument.m):

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
[[tv window] endEditingFor:nil]; // tv is my IBOutlet to an NSTableView

return [NSKeyedArchiver archivedDataWithRootObject:employees];
}

这是堆栈跟踪(谢谢,H2CO3):

(gdb) bt
#0 0x00007fff858da104 in objc_msgSend_vtable13 ()
#1 0x00007fff858dcff5 in objc_getProperty ()
#2 0x00000001000022cf in -[Person name] (self=0x1001d4480, _cmd=0x7fff89d1a790) at /Users/mauritsfriedrichkoelmans/Desktop/RaiseMan1/Person.m:14
#3 0x00007fff86e79674 in -[NSObject(NSKeyValueCoding) valueForKey:] ()
#4 0x00007fff86e7cfb8 in -[NSObject(NSKeyValueCoding) valueForKeyPath:] ()
#5 0x00007fff897e10df in -[NSBinder valueForBinding:atIndex:resolveMarkersToPlaceholders:] ()
#6 0x00007fff89682aa2 in -[NSValueBinder _adjustObject:mode:observedController:observedKeyPath:context:editableState:adjustState:] ()
#7 0x00007fff897e0e75 in -[NSValueBinder updateTableColumnDataCell:forDisplayAtIndex:] ()
#8 0x00007fff897e0d83 in -[NSTextValueBinder updateTableColumnDataCell:forDisplayAtIndex:] ()
#9 0x00007fff897e0d30 in -[_NSBindingAdaptor tableColumn:willDisplayCell:row:] ()
#10 0x00007fff896cab00 in -[NSTableView preparedCellAtColumn:row:] ()
#11 0x00007fff896ca306 in -[NSTableView _dirtyVisibleCellsForKeyStateChange] ()
#12 0x00007fff896c9e96 in -[NSTableView _windowChangedKeyState] ()
#13 0x00007fff88ba0d3e in CFArrayApplyFunction ()
#14 0x00007fff89611478 in -[NSView _windowChangedKeyState] ()
#15 0x00007fff88ba0d3e in CFArrayApplyFunction ()
#16 0x00007fff89611478 in -[NSView _windowChangedKeyState] ()
#17 0x00007fff88ba0d3e in CFArrayApplyFunction ()
#18 0x00007fff89611478 in -[NSView _windowChangedKeyState] ()
#19 0x00007fff88ba0d3e in CFArrayApplyFunction ()
#20 0x00007fff89611478 in -[NSView _windowChangedKeyState] ()
#21 0x00007fff896c991f in -[NSFrameView _windowChangedKeyState] ()
#22 0x00007fff89611171 in -[NSWindow _setFrameNeedsDisplay:] ()
#23 0x00007fff8960f1d8 in -[NSWindow _makeKeyRegardlessOfVisibility] ()
#24 0x00007fff8960f13e in -[NSWindow makeKeyAndOrderFront:] ()
#25 0x00007fff89814401 in -[NSWindowController showWindow:] ()
#26 0x00007fff897e5c5c in -[NSDocument showWindows] ()
#27 0x00007fff899539c1 in -[NSDocumentController openDocumentWithContentsOfURL:display:error:] ()
#28 0x00007fff89953072 in -[NSDocumentController _openDocumentsWithContentsOfURLs:display:presentErrors:] ()
#29 0x00007fff8976eeda in -[NSApplication sendAction:to:from:] ()
#30 0x00007fff8979346a in -[NSMenuItem _corePerformAction] ()
#31 0x00007fff897931d4 in -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] ()
#32 0x00007fff89778e45 in -[NSMenu performKeyEquivalent:] ()
#33 0x00007fff89777bed in -[NSApplication _handleKeyEquivalent:] ()
#34 0x00007fff896486b9 in -[NSApplication sendEvent:] ()
#35 0x00007fff895df6de in -[NSApplication run] ()
#36 0x00007fff895d83b0 in NSApplicationMain ()
#37 0x0000000100001e9b in main (argc=1, argv=0x7fff5fbff660) at /Users/mauritsfriedrichkoelmans/Desktop/RaiseMan1/main.m:13
(gdb)

数据对象不是 nil,NSLog()编辑了它。

最佳答案

我自己解决了!

在书中,Person 类的 initWithCoder: 方法(我尝试解压的数组中充满了该类的实例)如下所示:

- (id)initWithCoder:(NSCoder *)coder {
[super init];
name = [coder decodeObjectForKey:@"name"];
expectedRaise = [coder decodeFloatForKey:@"expectedRaise"];
return self;
}

但它必须是:

- (id)initWithCoder:(NSCoder *)coder {
[super init];
self.name = [coder decodeObjectForKey:@"name"];
self.expectedRaise = [coder decodeFloatForKey:@"expectedRaise"];
return self;
}

关于objective-c - [NSKeyedUnarchiver unarchiveObjectWithData :]; 上的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13610343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com