Search code examples
iosobjective-cios7nsstringunrecognized-selector

iOS NSMutableAttributedString Crash With unrecognized selector


Help!

the crash happens at :

[emtionString replaceCharactersInRange:range withString:imageAndRangeDicArray[i][@"image"]];

When NSMutableAttributedString is replaced with attributeString

This is the stack trace:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableAttributedString _encodingCantBeStoredInEightBitCFString]: unrecognized selector sent to instance 0x7fe8f34f52f0'

    *** First throw call stack:
    (
        0   CoreFoundation                      0x0000000107e95f35 __exceptionPreprocess + 165
        1   libobjc.A.dylib                     0x0000000107b2ebb7 objc_exception_throw + 45
        2   CoreFoundation                      0x0000000107e9d04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
        3   CoreFoundation                      0x0000000107df527c ___forwarding___ + 988
        4   CoreFoundation                      0x0000000107df4e18 _CF_forwarding_prep_0 + 120
        5   CoreFoundation                      0x0000000107db8195 __CFStringCheckAndReplace + 597
        6   CoreFoundation                      0x0000000107db7f1b -[__NSCFString replaceCharactersInRange:withString:] + 27
        7   Foundation                          0x00000001076da63e -[NSConcreteMutableAttributedString replaceCharactersInRange:withString:] + 337          

Solution

  • What types are emtionString and imageAndRangeDicArray[i][@"image"]?

    According to the docs, that method wants an NSString as the withString.

    If you're passing in an NSAttributedString instead it won't work. (It seems like NSAttributedString should be a subclass of NSString but it's not.)