NSFontAttributeName vs NSAttributedStringKey.font

Mic*_*hel 8 ios swift3

我在使用一段时间的库中使用Swift代码时遇到了一些麻烦.它似乎与某种版本冲突有关,但我不确定.

这是代码:

let attribMsg = NSAttributedString(string: msg,
                               attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 23.0)])
Run Code Online (Sandbox Code Playgroud)

这是我从编译器得到的错误消息:

Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'
    Did you mean 'zone'?  Fix(button)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在不同的项目中使用此代码,我注意到在其中一些我没有得到错误消息,它都编译没有任何问题.

我还注意到,如果我用以下代码替换上面的代码:

let attribMsg = NSAttributedString(string: msg,
                               attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 23.0)])
Run Code Online (Sandbox Code Playgroud)

有问题的项目将起作用,而其他项目(以前正在工作)显示此另一条消息:

'NSFontAttributeName' has been renamed to 'NSAttributedStringKey.font'
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

换句话说,一些项目使用一种类型的代码,而另一些项目使用另一种类型.

不用说,每次从一个项目切换到另一个项目时,我都不想更改代码.

我改变项目部署目标的实验似乎并没有太大的区别.我的问题就出现了:处理这个问题的方法是什么?

nat*_*ter 32

这些项目可能是Swift的不同版本.

在Swift 4中,NSFontAttributeName已被替换为NSAttributedStringKey.font.