带有AttributedString的componentsJoinedByString导致NSException

时间:2014-12-14 17:56:10

标签: ios objective-c

我在加入属性字符串数组时遇到以下错误。

2014-12-14 12:57:22.280 Matchismo[57752:60b] -[NSConcreteAttributedString _encodingCantBeStoredInEightBitCFString]: unrecognized selector sent to instance 0x8e81e20
2014-12-14 12:57:22.322 Matchismo[57752:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteAttributedString _encodingCantBeStoredInEightBitCFString]: unrecognized selector sent to instance 0x8e81e20'
*** First throw call stack:
(
    0   CoreFoundation                      0x017fa1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x015798e5 objc_exception_throw + 44
    2   CoreFoundation                      0x01897243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x017ea50b ___forwarding___ + 1019
    4   CoreFoundation                      0x017ea0ee _CF_forwarding_prep_0 + 14
    5   CoreFoundation                      0x0177e4e6 CFStringAppend + 566
    6   CoreFoundation                      0x017ebf04 -[NSArray componentsJoinedByString:] + 532
    7   Matchismo                           0x00004d59 -[CardGameViewController updateTextLabel] + 345
    8   Matchismo                           0x00005334 -[CardGameViewController updateUI] + 1396
    9   Matchismo                           0x00004bdd -[CardGameViewController touchCard:] + 253
    10  libobjc.A.dylib                     0x0158b880 -[NSObject performSelector:withObject:withObject:] + 77
    11  UIKit                               0x0023b3b9 -[UIApplication sendAction:to:from:forEvent:] + 108
    12  UIKit                               0x0023b345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    13  UIKit                               0x0033cbd1 -[UIControl sendAction:to:forEvent:] + 66
    14  UIKit                               0x0033cfc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
    15  UIKit                               0x0033c243 -[UIControl touchesEnded:withEvent:] + 641
    16  UIKit                               0x0027addd -[UIWindow _sendTouchesForEvent:] + 852
    17  UIKit                               0x0027b9d1 -[UIWindow sendEvent:] + 1117
    18  UIKit                               0x0024d5f2 -[UIApplication sendEvent:] + 242
    19  UIKit                               0x00237353 _UIApplicationHandleEventQueue + 11455
    20  CoreFoundation                      0x0178377f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    21  CoreFoundation                      0x0178310b __CFRunLoopDoSources0 + 235
    22  CoreFoundation                      0x017a01ae __CFRunLoopRun + 910
    23  CoreFoundation                      0x0179f9d3 CFRunLoopRunSpecific + 467
    24  CoreFoundation                      0x0179f7eb CFRunLoopRunInMode + 123
    25  GraphicsServices                    0x037ee5ee GSEventRunModal + 192
    26  GraphicsServices                    0x037ee42b GSEventRun + 104
    27  UIKit                               0x00239f9b UIApplicationMain + 1225
    28  Matchismo                           0x00008d5d main + 141
    29  libdyld.dylib                       0x01e41701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

这是我的代码:

self.textLabel.text = [myArray componentsJoinedByString:@","];

我理解具有着色的属性字符串以及不能存储在NSString中的内容。什么应该是正确的代码。

附上NSAttributed字符串所包含的数据类型的屏幕截图。它们包含您在图像中看到的圆/正方形/三角形。

Image of the data in the attributed strings.

1 个答案:

答案 0 :(得分:0)

如果要在属性字符串中附加新字符串,可以直接使用appendAttributedString方法。但它总是会在属性字符串的末尾添加。

如果您的要求是添加到属性字符串的中间,并且您想要添加","在您的属性字符串的每个单词中也包含着色属性。因此,在这种情况下,您需要首先将您的属性字符串转换为字符串,然后您需要将您的单词存储在数组中,然后您需要迭代数组中的单词并将该字符串转换为属性字符串与属性,然后使用{{1使用","。就像那样你可以实现同样的目标。