类型 'NSAttributedStringKey'(又名 'NSString')没有成员 'font'

Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'

刚刚为 xcode 9 更新了 pods,我收到以下关于 Cosmos 的错误。

Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'

您必须将 swift 版本从 Xcode.

更新到 4.0

使用下面的步骤

1) 从项目经理那里选择你的项目 (左上角来自 Xcode)
2) 选择构建设置
3) 搜索 "Swift language"
4) 更改 "Swift Language Version " => Swift 4.0

注意:确保项目的 swift 语言版本。以下是如何 see/check 您的 swift 语言版本。


您有两个选项可以解决您的问题:

  1. 如果您的项目有 Swift versio 4.0
    - 您应该 choose/download POD 与您项目的 swift 语言兼容(与我分享 POD 信息和 swift 版本,以便我可以为您的 pod 库提供准确的 pod 版本适合项目)。

  2. 如果你的项目 swift 版本低于 4.0
    - 您需要将您的项目迁移到 Swift 4.0(如果您尚未迁移它)。这是参考问题和答案,如何从 swift(以下)<4.0 迁移到 4.0。


根据您在问题中添加的标签 - Swift3 是您当前的项目语言版本和 pod 'Cosmos','~> 12.0' 支持 swift 4.

pod 'Cosmos', '~> 12.0'

这是支持 Swift 4.0 以下版本的先前版本列表。

https://github.com/evgenyneu/Cosmos/releases

像这样尝试以前版本的 cosmos:

pod 'Cosmos', '~> 11.0.3'
// or
pod 'Cosmos', '~> 11.0.1'
// or
pod 'Cosmos', '~> 11.0.0

'

 attributes: [NSFontAttributeName: label.font]

swift 3.2 似乎没问题。 我修复了分页菜单 viewcontroller 库。

以我为例 我替换此代码

[NSAttributedStringKey.font : font]

通过下面的代码

.size(attributes: [NSFontAttributeName : font])

希望对您有所帮助。

您的 pod 可能有错误的 swift 版本,而不是项目设置。

要更改 pod swift 设置,请在文件导航器(屏幕左侧)中转到 Pods(蓝色图标)和 select 您遇到问题的 pod和。然后转到构建设置并转到 Swift 语言版本。 Select Swift 4.

let label = UILabel()
label.font = UIFont.preferredFont(forTextStyle: .subheadline)
let percentOff = NSMutableAttributedString(string: "\(productOff ?? "0")%OFF", 
attributes: [NSFontAttributeName: UIFont(name: "Montserrat-Regular", 
size: 13)!,NSForegroundColorAttributeName: UIColor.black,
 NSBackgroundColorAttributeName: UIColor.black])
    lblPriceDetail.attributedText = percentOff

这对我有用 swift 4.2