NSFontAttributeName vs NSAttributedStringKey.font

I am having problems with Swift code in a library that I have been using for some time. This seems to be due to some sort of version conflict, but I'm not sure.

Here is the code:

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) 

enter image description here

Using this code in another project, I noticed that on some of them I do not receive an error message, and all this compiles without any problems.

I also noticed that if I replaced the above code with the following:

 let attribMsg = NSAttributedString(string: msg, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 23.0)]) 

Projects with a problem will work, while others (previously working) show this other message:

 'NSFontAttributeName' has been renamed to 'NSAttributedStringKey.font' 

enter image description here

In other words, some projects work with one type of code, and some others work with another type.

Needless to say, I don’t want to change the code every time I switch from one project to another.

The experiments I did with changing the project deployment goal do not seem to matter much. So my question is: What is the way to deal with this problem?

+3
source share
1 answer

Projects are probably in different versions of Swift.

In Swift 4, NSFontAttributeName been replaced by NSAttributedStringKey.font .

+16
source

Source: https://habr.com/ru/post/1269006/


All Articles