代码之家  ›  专栏  ›  技术社区  ›  Abdullah Umer

NSAttributedString.Key.writingDirection上的swift 4.2崩溃

  •  2
  • Abdullah Umer  · 技术社区  · 7 年前

    我的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 回复  |  直到 7 年前
        1
  •  4
  •   Community Mohan Dere    6 年前

    文件 NSAttributedString.Key.writingDirection 国家:

    此属性的值是 NSArray 包含对象 NSNumber 对象,表示写入方向替代的嵌套级别,按从最外层到最内层的顺序排列。

    NSWritingDirectionAttributeName 常量是一个字符级属性,它为文本中包含显式双向控制字符提供了更高级别的替代方法。它是 NSAttributedString 相当于使用具有dir属性的bdo元素的HTML标记。

    数字对象 对于LRE、RLE、LRO或RLO以及它们的组合,对象应分别为0、1、2或3 NSWritingDirection.leftToRight NSWritingDirection.rightToLeft 具有 NSTextWritingDirectionEmbedding NSTextWritingDirectionOverride

    您至少需要提供一个阵列:

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