【发布时间】:2017-08-03 00:02:56
【问题描述】:
iOS 部署目标:iOS 9.3, 基础 SDK:最新 iOS (iOS 11.0), Xcode 9 测试版 4, 斯威夫特 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”,错误仍然存在。
Command-clicking 'NSKernAttributeName' 告诉我它的可用性是 iOS 6。但是,检查器还声称它的类型是 'NSAttributedStringKey',它只在 iOS 11 中可用。我不知道这是否只是 API 演示的人工制品,或者是一个东西,或者我缺少的东西。
问题:我如何使用NSKernAttributeName(或其现代化版本)向后兼容iOS 9 和Swift 4?
感谢您的帮助。
【问题讨论】:
标签: ios xcode swift4 xcode9-beta