NSAttributedStringKey gives an unresolved identifier error

I am following an online tutorial on creating a magazine type iOS application. I try to use NSAttributedStringKey but keep getting the error shown below. Any ideas?

This is the line of code causing the error:

let attrs = [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: font] as [NSAttributedStringKey : Any] 
+5
source share
4 answers

You are trying to use the iOS 11 API in versions prior to iOS 11 (probably iOS 10). I’m surprised you found a tutorial that already uses beta features!

In the meantime, try this.

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

and that should work.

+17
source

The code you are trying to use is the new API added in iOS 11. Since you are using Xcode 8, you are not using iOS 11. Therefore, you need to use the current (non-beta) API.

 let attrs = [NSForegroundColorAttributeName: color, NSFontAttributeName: font] 
+3
source

In this example, only iOS11 works.

 import UIKit class VC: UIViewController { @IBOutlet weak var usernameTxt: UITextField! @IBOutlet weak var emailTxt: UITextField! @IBOutlet weak var passTxt: UITextField! override func viewDidLoad() { super.viewDidLoad() setupView() } func setupView() { usernameTxt.attributedPlaceholder = NSAttributedString(string: "username", attributes: [NSAttributedStringKey.foregroundColor: smackPurplePlaceholder]) emailTxt.attributedPlaceholder = NSAttributedString(string: "email", attributes: [NSAttributedStringKey.foregroundColor: smackPurplePlaceholder]) passTxt.attributedPlaceholder = NSAttributedString(string: "password", attributes: [NSAttributedStringKey.foregroundColor: smackPurplePlaceholder]) } } 
+3
source

Projects are probably in different versions of Swift.

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

as stated here NSFontAttributeName vs NSAttributedStringKey.font

It must be confirmed whether it will work in versions smaller than ios11

+2
source

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


All Articles