gpt4 book ai didi

ios - Swift 4 - Xcode 9 beta 4 - NSKernAttributeName 与 NSAttributedStringKey

转载 作者:可可西里 更新时间:2023-11-01 06:20:00 28 4
gpt4 key购买 nike

iOS 部署目标:iOS 9.3,基础 SDK:最新的 iOS (iOS 11.0),Xcode 9 测试版 4, swift 4

以下代码在 Swift 3、Xcode 8 中构建和运行:

    let kern = (CTRunGetAttributes(run) as? [String : Any])?
[NSKernAttributeName] as? CGFloat ?? 0

但是,Swift 4 迁移器将其转换为:

    let kern = (CTRunGetAttributes(run) as? [String : Any])?
[NSAttributedStringKey.kern] as? CGFloat ?? 0

Xcode 9 现在提示:

Cannot subscript a value of type '[String : Any]' with an index of type 'NSAttributedStringKey'

根据检查员的说法,“NSAttributedStringKey”仅在 iOS 11 中可用。(请记住,我的目标是 iOS 9)。

如果我将“NSAttributedStringKey.kern”替换为“NSKernAttributeName”,错误仍然存​​在。

命令单击“NSKernAttributeName”告诉我它的可用性是 iOS 6。但是,检查器还声称它的类型是“NSAttributedStringKey”,它仅可用在 iOS 11 中。我不知道这是否仅仅是 API 演示的人工产物,或者是一个东西,或者我缺少的东西。

问题:如何在 Swift 4 中使用向后兼容 iOS 9 的 NSKernAttributeName(或其现代化版本)?

感谢您的帮助。

最佳答案

将字典键入 [NSAttributedStringKey : Any] 而不是 [String : Any]。它是 Swift 覆盖层中的所有糖分,因此它甚至可以在较旧的 macOS/OS X 版本上运行。

编辑:我应该稍微澄清一下我的最后一句话。 NSAttributedStringKey,以及 Swift 4 中所有其他新的“键”类型,只存在于 Swift 中,而不存在于 Objective-C 中。当带有 NSAttributedStringKey 键的字典桥接到 Objective-C 时,所有的键都将变成 NSString。由于您正在使用的实际基础、UIKit 等框架是用 C/Objective-C 编写的,这将“正常工作”,您不必担心文档声称 NSAttributedStringKey 仅适用于 10.11。

关于ios - Swift 4 - Xcode 9 beta 4 - NSKernAttributeName 与 NSAttributedStringKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45472799/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com