TextureGroup / Texture

Smooth asynchronous user interfaces for iOS apps.
https://texturegroup.org/
Other
7.99k stars 1.29k forks source link

ASTextNodeDelegate crash when no `_highlightedLinkAttributeName` or `_highlightedLinkAttributeValue` set #921

Open brod-ie opened 6 years ago

brod-ie commented 6 years ago

ASTextNodeDelegate's method textNode:longPressedLinkAttribute:value:atPoint:textRange will crash when no _highlightedLinkAttributeName or _highlightedLinkAttributeValue is set.

I use this method to detect long pressing of my cell's text. Not all of my NSAttributedString has the .link attribute set, I imagine this could be causing the crash.

Stack trace:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x00000001137762cf libswiftCore.dylib`swift_getObjectType + 47
    frame #1: 0x000000010a016d17 muzmatch`@objc ASChatCell.textNode(_:longPressedLinkAttribute:value:at:textRange:) at ASChatCell.swift:0
  * frame #2: 0x000000010b4995d6 AsyncDisplayKit`::-[ASTextNode _handleLongPress:](self=0x00007ffea0080a00, _cmd="_handleLongPress:", longPressRecognizer=0x00007ffe9bc38f10) at ASTextNode.mm:1062
    frame #3: 0x000000010de9b41b UIKit`-[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 57
    frame #4: 0x000000010dea41f0 UIKit`_UIGestureRecognizerSendTargetActions + 109
    frame #5: 0x000000010dea1a38 UIKit`_UIGestureRecognizerSendActions + 307
    frame #6: 0x000000010dea0c8c UIKit`-[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 859
    frame #7: 0x000000010de85cf0 UIKit`_UIGestureEnvironmentUpdate + 1329
    frame #8: 0x0000000112359607 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    frame #9: 0x000000011235955e CoreFoundation`__CFRunLoopDoObservers + 430
    frame #10: 0x000000011233db81 CoreFoundation`__CFRunLoopRun + 1537
    frame #11: 0x000000011233d30b CoreFoundation`CFRunLoopRunSpecific + 635
    frame #12: 0x00000001160bda73 GraphicsServices`GSEventRunModal + 62
    frame #13: 0x000000010d8a30b7 UIKit`UIApplicationMain + 159
    frame #14: 0x0000000109c2cea7 muzmatch`main at AppDelegate.swift:31
    frame #15: 0x00000001140dc955 libdyld.dylib`start + 1

As I don't want to set this when a link isn't specified within a text range, I've bodged the delegate to accommodate nil values:

if (_highlightedLinkAttributeName == nil) {
   _highlightedLinkAttributeName = @"";
}

if (_highlightedLinkAttributeValue == nil) {
  _highlightedLinkAttributeValue = @"";
}

You can see these changes in this PR.

brod-ie commented 6 years ago

Plot twist, you can also fix this method by setting the delegates parameters to incorrectly (according to the header I imagine) be optional:

screen shot 2018-05-16 at 12 50 47