NSAttributedStringKey.attachment 与 NSAttachmentAttributeName
NSAttributedStringKey.attachment versus NSAttachmentAttributeName
我在使用 NSAttributedStringKey.attachment 与 NSAttachmentAttributeName 时遇到问题。这是相关代码:
var key: Any?
if #available(iOS 11, *) {
key = NSAttributedStringKey.attachment
}
else {
key = NSAttachmentAttributeName
}
两件事中的一件正在发生。在我尝试使用此代码的实际位置(我自己设计的 Cococapod,部署目标为 iOS 8,现在使用 Xcode 9 构建),我收到错误消息:
Type 'NSAttributedStringKey' (aka 'NSString') has no member 'attachment'
或者,如果我只是创建一个新的示例项目并将部署目标设置为 iOS 8,我会得到:
'NSAttachmentAttributeName' has been renamed to 'NSAttributedStringKey.attachment'
这不是我期望 #available
的行为。想法?
String
与 struct
的区别在于 Swift 3(使用 String
,例如 NSAttachmentAttributeName
)和 Swift 4(使用结构静态属性,例如 NSAttributedStringKey.attachment
),而不是在 iOS <11 和 iOS >=11 之间。例如,您可以在 Swift 4 项目中的 iOS 的任何支持版本中使用 NSAttributedStringKey.attachment
和类似版本(例如,.attachment
自 iOS 7 起可用)。 #available
不适用,因为它是 Swift 语言版本差异而不是 OS 版本差异。
确保您的 pod 设置为正确的 Swift 版本,然后它应该可以按预期工作。您可以通过在项目顶部添加一个 .swift-version
文件来告诉 CocoaPods:
$ echo 4.0 >.swift-version
这个神奇的版本文件在去年的CocoaPods博客post中被提及:http://blog.cocoapods.org/CocoaPods-1.1.0/
我在使用 NSAttributedStringKey.attachment 与 NSAttachmentAttributeName 时遇到问题。这是相关代码:
var key: Any?
if #available(iOS 11, *) {
key = NSAttributedStringKey.attachment
}
else {
key = NSAttachmentAttributeName
}
两件事中的一件正在发生。在我尝试使用此代码的实际位置(我自己设计的 Cococapod,部署目标为 iOS 8,现在使用 Xcode 9 构建),我收到错误消息:
Type 'NSAttributedStringKey' (aka 'NSString') has no member 'attachment'
或者,如果我只是创建一个新的示例项目并将部署目标设置为 iOS 8,我会得到:
'NSAttachmentAttributeName' has been renamed to 'NSAttributedStringKey.attachment'
这不是我期望 #available
的行为。想法?
String
与 struct
的区别在于 Swift 3(使用 String
,例如 NSAttachmentAttributeName
)和 Swift 4(使用结构静态属性,例如 NSAttributedStringKey.attachment
),而不是在 iOS <11 和 iOS >=11 之间。例如,您可以在 Swift 4 项目中的 iOS 的任何支持版本中使用 NSAttributedStringKey.attachment
和类似版本(例如,.attachment
自 iOS 7 起可用)。 #available
不适用,因为它是 Swift 语言版本差异而不是 OS 版本差异。
确保您的 pod 设置为正确的 Swift 版本,然后它应该可以按预期工作。您可以通过在项目顶部添加一个 .swift-version
文件来告诉 CocoaPods:
$ echo 4.0 >.swift-version
这个神奇的版本文件在去年的CocoaPods博客post中被提及:http://blog.cocoapods.org/CocoaPods-1.1.0/