objective-c – 在NSTextStorage中混淆编码异常

objective-c – 在NSTextStorage中混淆编码异常,第1张

概述我有一个NSDocument子类用于简单的文本编辑器(使用Lion的新的基于文档的应用程序模板,几乎没有自定义),我遇到一个奇怪的错误,将文件内容加载到文本存储中。 这是我的代码: - (void)loadTextContentIntoStorage{ if (!self.textStorage || !textContentToLoad) return; ... [se 我有一个NSdocument子类用于简单的文本编辑器(使用lion的新的基于文档的应用程序模板,几乎没有自定义),我遇到一个奇怪的错误,将文件内容加载到文本存储中。

这是我的代码:

- (voID)loadTextContentIntoStorage{  if (!self.textStorage || !textContentToload)    return;  ...  [self.textStorage beginEditing];//  NSLog(@"storage: %@ length: %lu textContent: %@",self.textStorage,(unsigned long)self.textStorage.length,textContentToload);//  [self.textStorage replaceCharactersInRange:NSMakeRange(0,self.textStorage.length) withString:textContentToload];  [self.textStorage replaceCharactersInRange:NSMakeRange(0,0) withString:@"hello world"];  ..  [self.textStorage endEditing];}

当我发生错误时:

>在Xcode中运行应用程序(作为调试版本)
>打开任何文档
>退出应用程序(不关闭文档)
>再从Xcode运行应用程序

它在-replaceCharactersInRange中崩溃:withString:与“无法将字符串0x10004d430中的字节转换为_NSCStringEnCoding”。

但它只发生在应用程序的第二次启动(第三次启动不会崩溃,它会自动重新打开它崩溃的文档试图打开上一次)。它也只有当我从Xcode运行应用程序时才会发生。发布版本从未在启动时崩溃。

我认为这可能是自动保存系统的编码问题,但是当我注释掉该代码并且将@“hello world”字符串加载到文本视图(如上面的代码所示)时,它甚至会崩溃。同样,注释出来的NSLog()并没有显示任何奇怪的东西。文本存储是有效的(从xib文件加载),文本存储长度为0,textContent是要打开的文件的内容。

—编辑—

我已经学到这个问题在某种程度上与com.apple.security.app-sandBox的权利有关。如果启用了权利/沙箱,那么我的应用程序不会崩溃。如果任一权利或应用程序沙箱功能被禁用,则我的应用程序在每次启动时都会崩溃,尝试恢复以前打开的文档。

我在Xcode内部进行构建/运行时才注意到它崩溃,因为这是我唯一的禁用沙箱的构建配置。

— / EDIT —

有人有什么想法吗?完整的例外如下,完整的源代码在github:https://github.com/abhibeckert/Dux/blob/master/Dux/DuxTextView.m上

*** Terminating app due to uncaught exception 'NSInternalinconsistencyException',reason: 'Unable to convert bytes in string 0x10004d430 to _NSCStringEnCoding'*** First throw call stack:(    0   CoreFoundation                      0x00007fff84afb286 __exceptionPreprocess + 198    1   libobjc.A.dylib                     0x00007fff88991d5e objc_exception_throw + 43    2   CoreFoundation                      0x00007fff84afb0ba +[NSException raise:format:arguments:] + 106    3   CoreFoundation                      0x00007fff84afb044 +[NSException raise:format:] + 116    4   Foundation                          0x00007fff835bfae4 copyFromStringToStorage + 262    5   Foundation                          0x00007fff835bf979 -[NSBigMutableString replaceCharactersInRange:withString:] + 1000    6   Foundation                          0x00007fff835bc3f7 -[NSConcreteMutableAttributedString replaceCharactersInRange:withString:] + 375    7   AppKit                              0x00007fff86149e14 -[NSConcreteTextStorage replaceCharactersInRange:withString:] + 81    8   Dux                                 0x0000000100002f9a -[MyTextdocument loadTextContentIntoStorage] + 1338    9   Dux                                 0x00000001000022a0 -[MyTextdocument windowControllerDIDLoadNib:] + 640    10  AppKit                              0x00007fff860f1328 -[NSWindowController _windowDIDLoad] + 667    11  AppKit                              0x00007fff860e89a3 -[NSWindowController window] + 109    12  AppKit                              0x00007fff8615d761 -[NSdocument windowForSheet] + 86    13  AppKit                              0x00007fff860e82c4 -[NSdocument _shouldShowautosavebuttonForWindow:] + 50    14  AppKit                              0x00007fff860e7fbb -[NSWindowController setdocument:] + 237    15  AppKit                              0x00007fff8629c9b6 -[NSdocument makeWindowControllers] + 139    16  AppKit                              0x00007fff8615d555 -[NSdocument(NSPersistentUISupport) restoredocumentWindowWithIDentifIEr:state:completionHandler:] + 90    17  AppKit                              0x00007fff8615d4aa -[NSdocumentControllerPersistentRestoration loadeddocument:forautoID:] + 179    18  AppKit                              0x00007fff8615cfbe __-[NSdocumentController reopendocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_8 + 187    19  AppKit                              0x00007fff86148e14 __-[NSdocumentController reopendocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_5 + 163    20  AppKit                              0x00007fff86148d5f __-[NSdocumentController reopendocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_4 + 697    21  AppKit                              0x00007fff86148aa1 -[NSdocumentController _opendocumentWithContentsOfURL:usingProcedure:] + 530    22  AppKit                              0x00007fff8614868d __-[NSdocumentController reopendocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_3 + 242    23  libdispatch.dylib                   0x00007fff8bbbe8ba _dispatch_call_block_and_release + 18    24  libdispatch.dylib                   0x00007fff8bbc072a _dispatch_main_queue_callback_4CF + 308    25  CoreFoundation                      0x00007fff84a904dc __CFRunLoopRun + 1724    26  CoreFoundation                      0x00007fff84a8fae6 CFRunLoopRunspecific + 230    27  HIToolBox                           0x00007fff8852f3d3 runcurrentEventLoopInMode + 277    28  HIToolBox                           0x00007fff8853663d ReceiveNextEventCommon + 355    29  HIToolBox                           0x00007fff885364ca BlockUntilNextEventMatchingListInMode + 62    30  AppKit                              0x00007fff85ef23f1 _DPSNextEvent + 659    31  AppKit                              0x00007fff85ef1cf5 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 135    32  AppKit                              0x00007fff85eee62d -[NSApplication run] + 470    33  AppKit                              0x00007fff8616d80c NSApplicationMain + 867    34  Dux                                 0x0000000100001e32 main + 34    35  Dux                                 0x0000000100001e04 start + 52    36  ???                                 0x0000000000000003 0x0 + 3)
解决方法 我承认这个问题让我痛苦了不少时间。我想我终于找到了一个责备。

查看GNUstep code that emulates this behavior,我发现以下源代码:

if (enc == NSASCIIStringEnCoding    && isByteEnCoding(internalEnCoding))    {      unsigned  i;      if (bytes > self->_count)    {      bytes = self->_count;    }      for (i = 0; i < bytes; i++)    {      unsigned char c = self->_contents.c[i];      if (c > 127)        {          [NSException raise: NSCharacterConversionException              format: @"unable to convert to enCoding"];        }      buffer[i] = c;    }      buffer[bytes] = 'reason: 'Unable to convert bytes in string 0x10004d430 to';      if (bytes < self->_count)    {      return NO;    }      return YES;    }...

显然,此代码与您的错误消息不是一一对应的。但是,我从你的错误中注意到:

_NSCStringEnCoding'

那么,we all know what a C String is,如果你使用错误的编码方法(例如,WIDe,其中ASCII是预期的,反之亦然),你将最终得出这个问题。因此:您是否尝试在应用程序中更改ASCII和WIDe数据类型以解决此问题?

由于这种行为发生的常见原因,很可能表明它是应用程序沙箱的处理案例。但是,我仍然找不到文件。这应该是沙盒明确指出的,就像我在问题勘误表中提出的那样。

总结

以上是内存溢出为你收集整理的objective-c – 在NSTextStorage中混淆编码异常全部内容,希望文章能够帮你解决objective-c – 在NSTextStorage中混淆编码异常所遇到的程序开发问题。

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

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

原文地址:https://54852.com/web/1039369.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存