Swift 4.2在NSAttributedString.Key.writingDirection上崩溃

时间:2019-01-24 15:16:38

标签: ios swift

我的iOS应用程序中包含以下代码:

let subAttr: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : font,
                                                   NSAttributedString.Key.foregroundColor : color,
                                                   NSAttributedString.Key.writingDirection : NSWritingDirection.leftToRight]

这导致崩溃:

2019-01-24 18:12:04.425440+0300 Haraj[5461:1355738] -[_SwiftValue count]: unrecognized selector sent to instance 0x281c752f0
2019-01-24 18:12:04.425619+0300 Haraj[5461:1355738] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue count]: unrecognized selector sent to instance 0x281c752f0'
*** First throw call stack:
(0x1f5327ef8 0x1f44f5a40 0x1f523f154 0x1f532d810 0x1f532f4bc 0x1f525b954 0x1f90f5e3c 0x1f90f5cf4 0x1f9049030 0x1f9048fb8 0x1f9003050 0x1f9016e28 0x1f9016b6c 0x1f9004070 0x1ff8d7e2c 0x1ff956e38 0x1ff8dc0b0 0x221be3e24 0x221be446c 0x222536c4c 0x22253740c 0x222537068 0x222541210 0x221be38f0 0x22253fd50 0x222540200 0x2225400d4 0x1f5cc551c 0x22254045c 0x2225400d4 0x2225409a4 0x2225405c4 0x2225415e4 0x2225e7e90 0x2225fbfa8 0x1f9909a34 0x1f990e9c4 0x2225e7430 0x2225edfe0 0x22237e130 0x22237e478 0x222349bfc 0x222367ae8 0x2225fbf44 0x1f9909a34 0x1f990e9c4 0x2225e7430 0x221b019bc 0x221afc0b0 0x221afa280 0x2225b7314 0x2225b75d0 0x2225c5490 0x2225c0ecc 0x2225b7058 0x221b027cc 0x221b02da8 0x221b040a8 0x221ae6298 0x2225fbf44 0x1f9909a34 0x1f990e9c4 0x1f986d9d4 0x1f989c2f4 0x222181bf4 0x1f52b5b94 0x1f52b0828 0x1f52b0dc8 0x1f52b05b8 0x1f7524584 0x222158bc8 0x10257be3c 0x1f4d70b94)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) po 0x281c752f0
__C.NSWritingDirection

(lldb) 

这在Swift 3中工作正常,但我最近迁移到了Swift 4.2。有什么帮助吗?

1 个答案:

答案 0 :(得分:4)

NSAttributedString.Key.writingDirection的文档说明:

  

此属性的值是一个NSArray对象,其中包含NSNumber个对象,这些对象代表从最外面到最里面的顺序的嵌套嵌套重写级别。

     

此属性提供一种方法来替代默认的双向文本算法,等效于将Unicode bidi控制字符LRE,RLE,LRO或RLO与PDF配对使用,但作为高级属性。 (有关Unicode bidi格式代码的信息,请参见Unicode标准附件#9。)NSWritingDirectionAttributeName常量是字符级别的属性,它提供了更高级别的替代,可以在文本中包含显式双向控制字符。这与使用带有dir属性的bdo元素的HTML标记等效NSAttributedString

     

对于LRE,RLE,LRO或RLO,NSNumber对象的值应分别为0、1、2或3,并且NSWritingDirection.leftToRightNSWritingDirection.rightToLeft的组合应与NSTextWritingDirectionEmbeddingNSTextWritingDirectionOverride,如表1所示。

至少需要提供一个数组:

let subAttr: [NSAttributedString.Key : Any] = [
    .font : font,
    .foregroundColor : color,
    .writingDirection : [ NSNumber(value: NSWritingDirection.leftToRight.rawValue) ]
]