Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSAttributedStringKey giving an unresolved identifier error

Tags:

xcode

ios

swift

I'm following an online tutorial to build a magazine type iOS application. I'm attempting to use NSAttributedStringKey but keep getting the error shown below. Any ideas?

This is the line of code that is causing the error:

let attrs = [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: font] as [NSAttributedStringKey : Any]
like image 582
Johnm95 Avatar asked Jun 20 '17 00:06

Johnm95


2 Answers

The projects are probably in different versions of Swift.

In Swift 4, NSFontAttributeName has been replaced with NSAttributedStringKey.font.

as stated here NSFontAttributeName vs NSAttributedStringKey.font

Need to confirm whether it will work on versions less than ios11

like image 62
Tibin Thomas Avatar answered Nov 15 '22 19:11

Tibin Thomas


You are trying to use an iOS 11 API on a pre-iOS 11 version (likely iOS 10). Surprised that you found a tutorial already using beta features!

In the meantime, try this.

let attrs = [NSForegroundColorAttributeName: color, NSFontAttributeName: font]

and that should work.

like image 21
chrismanderson Avatar answered Nov 15 '22 18:11

chrismanderson