swift 4.2 在 NSAttributedString.Key.writingDirection 上崩溃

Abd*_*mer 3 ios swift

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

let subAttr: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : font,
                                                   NSAttributedString.Key.foregroundColor : color,
                                                   NSAttributedString.Key.writingDirection : NSWritingDirection.leftToRight]
Run Code Online (Sandbox Code Playgroud)

这导致了崩溃:

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) 
Run Code Online (Sandbox Code Playgroud)

这在 中运行良好Swift 3,但我最近迁移到了Swift 4.2. 有什么帮助吗?

rma*_*ddy 6

状态的文档NSAttributedString.Key.writingDirection

该属性的值是一个NSArray对象,其中包含NSNumber表示书写方向覆盖的嵌套级别的对象,按从最外层到最内层的顺序排列。

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

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

至少您需要提供一个数组:

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