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

objective-c – [NSKeyedUnarchiver unarchiveObjectWithData:]上的EXC_BAD_ACCESS;,第1张

概述我设定赏金后找到了答案.如果有人对我的方法有一些有用的评论(我不知道它是否是’正确的’解决方案)他/她可以获得赏金.如果您打算回答我的问题,请发表评论,以便我可以改进未来的问题. 我目前正在学习Objective-C,其中包括Cocoa Programming For Mac OS X,它在第10章介绍了归档.I(AFAIK)确实完成了作者希望我做的事情,但是当打开文件并因此取消归档应用程序时崩溃 我设定赏金后找到了答案.如果有人对我的方法有一些有用的评论(我不知道它是否是’正确的’解决方案)他/她可以获得赏金.如果您打算回答我的问题,请发表评论,以便我可以改进未来的问题.

我目前正在学习Objective-C,其中包括Cocoa Programming For Mac OS X,它在第10章介绍了归档.I(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];}

这是堆栈跟踪(Thanks,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)

NSLog()编辑它的数据对象不是nil.

解决方法 我自己解决了!

在本书中,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;全部内容,希望文章能够帮你解决objective-c – [NSKeyedUnarchiver unarchiveObjectWithData:]上的EXC_BAD_ACCESS;所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1220965.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存