Ios Swift中重命名属性的回退

Ios Swift中重命名属性的回退,ios,swift,uikit,swift4,xcode9,Ios,Swift,Uikit,Swift4,Xcode9,我很难找到像这样的事情的退路 UIFontDescriptor.AttributeName NSAttributedStringKey.foregroundColor 此问题出现在XCode 9中的Swift 4中。目前,我要做什么 #if swift(>=4.0) if #available(iOS 11, *) { [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white] } els

我很难找到像这样的事情的退路

UIFontDescriptor.AttributeName
NSAttributedStringKey.foregroundColor
此问题出现在XCode 9中的Swift 4中。目前,我要做什么

#if swift(>=4.0)
    if #available(iOS 11, *) {
        [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
    } else {
        ["NSColor": UIColor.white]
    }
 #else 
       [NSForegroundColorAttributeName.rawValue: UIColor.white]

但是,对于UIFontDescriptor,我找不到任何适用于iOS 8的东西。另外,如果你能改进这项技术,那也会很棒。

在Swift 4中,你使用
UIFontDescriptor.AttributeName.xxx
NSAttributedStringKey.yyy,其中
xxx
yyy`是所需的名称

在Swift 3中,您使用
UIFontDescriptorXXXAttribute
NSYYYAttributeName
,其中
XXX
YYY
是所需的名称

只要您使用的密钥存在于iOS 8中,Swift 4代码就可以像iOS 11、10、9和8一样正常工作。您不需要
#if
#可用的

这意味着,对于部署目标为iOS 8或更高版本的应用程序,可以在Xcode 9中使用以下代码:

let fontDesc = UIFontDescriptor()
fontDesc.addingAttributes([ .name: "Helvetica" ])
let font = UIFont(descriptor: fontDesc, size: 14)
let dict = [ NSAttributedStringKey.foregroundColor: UIColor.green, NSAttributedStringKey.font: font ]
let attrStr = NSAttributedString(string: "Hello", attributes: dict)
如果您需要使用Xcode 9和Xcode 8(Swift 4和Swift 3)构建此代码,则需要执行以下操作:

let fontDesc = UIFontDescriptor()
#if swift(>=4.0)
    fontDesc.addingAttributes([ .name: "Helvetica" ])
#else
    fontDesc.addingAttributes([ UIFontDescriptorNameAttribute: "Helvetica" ])
#endif
let font = UIFont(descriptor: fontDesc, size: 14)
#if swift(>=4.0)
    var dict = [ NSAttributedStringKey.foregroundColor: UIColor.yellow, NSAttributedStringKey.font: font ]
#else
    var dict = [ NSForegroundColorAttributeName: UIColor.yellow, NSFontAttributeName: font ]
#endif

let attrStr = NSAttributedString(string: "Hello", attributes: dict)

请注意,在任何一组代码中,您都不会对任何键使用
rawValue
。您应该注意硬编码键字符串。使用提供的常量。

在Swift 4中,您使用
UIFontDescriptor.AttributeName.xxx
NSAttributedStringKey.yyy,其中
xxx
yyy`是所需的名称

在Swift 3中,您使用
UIFontDescriptorXXXAttribute
NSYYYAttributeName
,其中
XXX
YYY
是所需的名称

只要您使用的密钥存在于iOS 8中,Swift 4代码就可以像iOS 11、10、9和8一样正常工作。您不需要
#if
#可用的

这意味着,对于部署目标为iOS 8或更高版本的应用程序,可以在Xcode 9中使用以下代码:

let fontDesc = UIFontDescriptor()
fontDesc.addingAttributes([ .name: "Helvetica" ])
let font = UIFont(descriptor: fontDesc, size: 14)
let dict = [ NSAttributedStringKey.foregroundColor: UIColor.green, NSAttributedStringKey.font: font ]
let attrStr = NSAttributedString(string: "Hello", attributes: dict)
如果您需要使用Xcode 9和Xcode 8(Swift 4和Swift 3)构建此代码,则需要执行以下操作:

let fontDesc = UIFontDescriptor()
#if swift(>=4.0)
    fontDesc.addingAttributes([ .name: "Helvetica" ])
#else
    fontDesc.addingAttributes([ UIFontDescriptorNameAttribute: "Helvetica" ])
#endif
let font = UIFont(descriptor: fontDesc, size: 14)
#if swift(>=4.0)
    var dict = [ NSAttributedStringKey.foregroundColor: UIColor.yellow, NSAttributedStringKey.font: font ]
#else
    var dict = [ NSForegroundColorAttributeName: UIColor.yellow, NSFontAttributeName: font ]
#endif

let attrStr = NSAttributedString(string: "Hello", attributes: dict)

请注意,在任何一组代码中,您都不会对任何键使用
rawValue
。您应该注意硬编码键字符串。使用提供的常量。

这确实令人困惑。你打算用这段代码做什么?@rmaddy试图找到适用于Swift 4、Xcode 9但也适用于iOS 8的东西。例如,假设您想要UIBarItem上的attributedString。调用setTitleTextAttributes,但需要返回[NSAttributedStringKey:Any]的字典,但在iOS 11中添加了NSAttributedStringKey。对于所有iOS版本8+,您将如何执行此操作。如果您在代码中经常执行此操作,则可以创建自定义枚举。构建一个接受此枚举并将其转换为字典的函数。只有函数将包含#if检查。这在以后有更好的解决方案时会更容易改变。你能举个例子吗?这真是令人困惑。你打算用这段代码做什么?@rmaddy试图找到适用于Swift 4、Xcode 9但也适用于iOS 8的东西。例如,假设您想要UIBarItem上的attributedString。调用setTitleTextAttributes,但需要返回[NSAttributedStringKey:Any]的字典,但在iOS 11中添加了NSAttributedStringKey。对于所有iOS版本8+,您将如何执行此操作。如果您在代码中经常执行此操作,则可以创建自定义枚举。构建一个接受此枚举并将其转换为字典的函数。只有函数将包含#if检查。如果有更好的解决方案,这将更容易在以后更改。您能否在文档中发布一个示例,说明iOS 11中添加了NSAttributedStringKey,但所有属性都适用于iOS 6或更高版本,这是否意味着只要我使用该属性就可以了?是的,
NSAttributedStringKey
是在iOS 11中添加的,但它们所表示的值等于先前API中的相应值。这就是为什么您可以在Xcode 9中使用
NSAttributedStringKey.foregroundColor
,并让它在旧版本的iOS中工作。在运行时,它是相同的值。使用我发布的第一组代码,它就会工作。我在iOS 8模拟器中测试了该代码,我可以看到彩色的Helvetica文本。在文档中说,iOS 11中添加了NSAttributedStringKey,但所有属性都适用于iOS 6或更早版本,这是否意味着只要我使用该属性就可以了?是的,
NSAttributedStringKey
是在iOS 11中添加的,但它们所表示的值等于先前API中的相应值。这就是为什么您可以在Xcode 9中使用
NSAttributedStringKey.foregroundColor
,并让它在旧版本的iOS中工作。在运行时,它是相同的值。使用我发布的第一组代码,它就会工作。我在iOS 8模拟器中测试了该代码,我可以看到彩色的Helvetica文本。