简体   繁体   English

NSTextStorage的子类导致访问链接崩溃

[英]Subclass of NSTextStorage causes crash on access link

I'm building a custom NSTextStorage class to find&match custom user input and process it inside my app. 我正在构建一个自定义的NSTextStorage类来查找和匹配自定义用户输入并在我的应用程序中处理它。 So I followed a couple of tutorials, most notably objc.io's , and created a subclass and set it's layoutManager to the same of the UITextView created via nib: 所以我遵循了几个教程,最着名的是objc.io ,并创建了一个子类,并将它的layoutManager设置为通过nib创建的UITextView

-(void)setCustomTextStorage:(NSTextStorage *)customTextStorage
{
    if (_customTextStorage != customTextStorage)
    {
        _customTextStorage = customTextStorage;
        [_customTextStorage addLayoutManager:self.textView.layoutManager];
    }
}

Then on override processEditing for the NSTextStorage subclass and add the NSLinkAttributeName where appropriate. 然后在覆盖processEditing for NSTextStorage子类并在适当的位置添加NSLinkAttributeName

BTW, The sample app is available here , make sure to checkout the custom-text-storage branch or just read the diff for this problem here 顺便说一下这里有示例应用程序,请务必签出自custom-text-storage分支,或者只是在这里阅读差异这个问题

Now, when the user taps on the actual link, I get an exception that crashes the app: 现在,当用户点击实际链接时,我得到一个崩溃应用程序的异常:

2014-08-20 23:06:45.223 JSQMessages[63170:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSConcreteTextStorage attribute:atIndex:effectiveRange:]: Range or index out of bounds'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001025d2495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001021a999e objc_exception_throw + 43
    2   CoreFoundation                      0x00000001025d22ad +[NSException raise:format:] + 205
    3   UIFoundation                        0x000000010558273f -[NSConcreteTextStorage attribute:atIndex:effectiveRange:] + 115
    4   UIKit                               0x00000001013d401b -[UITextView(LinkInteraction) _interactableItemAtPoint:] + 789
    5   UIKit                               0x00000001013d59a4 -[UITextView(LinkInteraction) willInteractWithLinkAtPoint:] + 17
    6   UIKit                               0x000000010108801b -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) gestureRecognizerShouldBegin:] + 305
    7   UIKit                               0x000000010107fc7b -[UIGestureRecognizer _shouldBegin] + 1026
    8   UIKit                               0x000000010107cdfd -[UIGestureRecognizer setState:] + 183
    9   UIKit                               0x0000000100ebcd3f -[UIDelayedAction timerFired:] + 68
    10  Foundation                          0x0000000101d93e14 __NSFireTimer + 83
    11  CoreFoundation                      0x0000000102594c34 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    12  CoreFoundation                      0x00000001025947b2 __CFRunLoopDoTimer + 962
    13  CoreFoundation                      0x000000010257d7be __CFRunLoopRun + 1614
    14  CoreFoundation                      0x000000010257cd83 CFRunLoopRunSpecific + 467
    15  GraphicsServices                    0x0000000103c2ef04 GSEventRunModal + 161
    16  UIKit                               0x0000000100d56e33 UIApplicationMain + 1010
    17  JSQMessages                         0x0000000100014ea3 main + 115
    18  libdyld.dylib                       0x000000010339f5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Which is weird, because I set up a breakpoint there and the backingStore is returning a perfectly valid NSDictionary with the attributes. 这很奇怪,因为我在那里设置了一个断点,而backingStore正在返回一个带有属性的完全有效的NSDictionary

I'll gladly buy a beer to whoever will give me a hand with this maddening issue. 我很乐意买一杯啤酒给那些能帮我解决这个疯狂问题的人。

Note: Also reproducible on iOS 8, so I don't think it's an iOS bug 注意:在iOS 8上也可以重现,所以我不认为这是一个iOS错误

Encountered with the same problem. 遇到同样的问题。 The only solution I've found for now is to create a full stack programmatically instead of using nib: 我现在发现的唯一解决方案是以编程方式创建完整堆栈而不是使用nib:

self.textStorage = [[CustomTextStorage alloc] init];

NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[self.textStorage addLayoutManager:layoutManager];

NSTextContainer *textContainer = [[NSTextContainer alloc] init];
[layoutManager addTextContainer:textContainer];

UITextView *textView = [[UITextView alloc] initWithFrame:frame textContainer:textContainer];
textView.delegate = self;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM