问题描述
我在使用一段时间的库中使用Swift代码时遇到了一些麻烦。
似乎与某种版本冲突有关,但我不确定。
I am having some trouble with Swift code in a library I have been using for a while.It seems related to some kind of version conflict, but I am not sure.
以下是代码:
let attribMsg = NSAttributedString(string: msg,
attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 23.0)])
以下是我从编译器获得的错误消息:
Here is the error message I get from the compiler:
Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'
Did you mean 'zone'? Fix(button)
在不同的项目中使用此代码,我注意到其中一些我没有得到错误消息,所有编译都没有任何问题。
Using this code in different project I noticed that on some of them I do not get the error message and it all compiles without any problem.
我还注意到,如果我用以下代码替换上面的代码:
I also noticed that if I replace the above code by the following:
let attribMsg = NSAttributedString(string: msg,
attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 23.0)])
项目一个问题将起作用,而其他人(以前工作)显示另一条消息:
The projects with a problem will work while the others(previously working) show this other message:
'NSFontAttributeName' has been renamed to 'NSAttributedStringKey.font'
换句话说,某些项目使用一种类型的代码,而另一些项目则使用另一种类型。
In other words some projects work with one type of code and some others work with the other type.
不用说我每次从一个项目切换到另一个项目时都不想更改代码。
Needless to say that I do not want to change the code each time I switch from one project to another.
我改变项目的部署目标的实验似乎没有太大的区别。
所以我的问题是:处理这个问题的方法是什么?
The experiments I made changing the Deployment Target of the project do not seem to make much difference.So comes my question: What is the way to handle this issue?
推荐答案
这些项目可能是不同版本的Swift。
The projects are probably in different versions of Swift.
在Swift 4中, NSFontAttributeName
已被<$ c替换$ c> NSAttributedStringKey.font 。
In Swift 4, NSFontAttributeName
has been replaced with NSAttributedStringKey.font
.
这篇关于NSFontAttributeName vs NSAttributedStringKey.font的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!