我正在将一些代码从Swift 4.0移到4.2,而我在使用UIAccessibilitySpeechAttributePitch时遇到了麻烦。在我的Swift 4.0文件中,我使用了:
NSAttributedStringKey(rawValue: UIAccessibilitySpeechAttributePitch)
即使在Swift 4.0中,由于某些原因,我无法直接将它用作NSAttributedStringKey.accessibilitySpeechPitch
,但我可以通过初始化使用原始值来解决这个问题。我似乎无法在Swift 4.2中做到这一点。
如果我试试
NSAttributedString.Key(rawValue: UIAccessibilitySpeechAttributePitch)
在Swift 4.2中,我明白了
无效的初始化程序调用,具有相同类型的“NSAttributedString.Key”作为参数
所以很明显UIAccessibilitySpeechAttributePitch
被认为是NSAttributedString.Key
。但是,如果我直接使用它,我得到:
'UIAccessibilitySpeechAttributePitch'已重命名为'NSAttributedString.Key.accessibilitySpeechPitch'
建议修复
将'UIAccessibilitySpeechAttributePitch'替换为'NSAttributedString.Key.accessibilitySpeechPitch'
所以我点击'修复'按钮将其更改为NSAttributedString.Key.accessibilitySpeechPitch
,然后我得到
类型'NSAttributedString.Key'没有成员'accessibilitySpeechPitch'
如果我命令点击UIAccessibilitySpeechAttributePitch
而不先将其更改为NSAttributedString.Key.accessibilitySpeechPitch
,我将在UIKit的UIAccessibilityConstants中获得此声明:
extension NSAttributedString.Key {
//other stuff
@available(iOS 7.0, *)
public static let accessibilitySpeechPitch: NSAttributedString.Key
//other stuff
}
所以,它显然存在,但是,我的代码如何使用它?除了UIKit之外,我还需要为辅助功能常量导入额外的东西吗?
编辑:我的实际代码(只更改了一些变量名称)如下:
static let someColour=#colorLiteral(red: 1, green: 0.5763723254, blue: 0, alpha: 0.5)
static let attributes=[
NSAttributedString.Key.backgroundColor: someColour,
NSAttributedString.Key.strokeWidth: -3.0,
NSAttributedString.Key.accessibilitySpeechPitch: 1.3
]
as [NSAttributedString.Key : Any]
我试过干净的建筑。我正在使用Xcode版本10.0(10A255)
尝试
导入UIKit
因为UIKit -> NSAttribute
文件中存在可访问性语音间距,所以你可以在extension NSAttributedString.Key
中看到它
有效