NSAttributedStringKey给出未解析的标识符错误

我正在按照在线教程构建一个杂志类型的iOS应用程序。 我正在尝试使用NSAttributedStringKey,但不断收到如下所示的错误。 有任何想法吗?

这是导致错误的代码行:

let attrs = [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: font] as [NSAttributedStringKey : Any] 

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

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

如此处所述NSFontAttributeName vs NSAttributedStringKey.font

需要确认它是否适用于低于ios11的版本

您正尝试在iOS 11之前的版本(可能是iOS 10)上使用iOS 11 API。 很惊讶您发现已经使用测试版function的教程!

在此期间,试试这个。

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

这应该工作。

您尝试使用的代码是在iOS 11中添加的新API。由于您使用的是Xcode 8,因此您不使用iOS 11.因此您需要使用当前(非beta)API。

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

Swift 4.1和Xcode 9.3更改归属键值。

 let strName = "Hello Stackoverflow" let string_to_color2 = "Hello" let attributedString1 = NSMutableAttributedString(string:strName) let range2 = (strName as NSString).range(of: string_to_color2) attributedString1.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red , range: range2) lblTest.attributedText = attributedString1 

你好将是红色的。

此示例仅适用于iOS11。

 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]) } } 

Swift 4.x

  // MARK: - Deal with the empty data set // Add title for empty dataset func title(forEmptyDataSet _: UIScrollView!) -> NSAttributedString! { let str = "Welcome" let attrs = [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline)] return NSAttributedString(string: str, attributes: attrs) } // Add description/subtitle on empty dataset func description(forEmptyDataSet _: UIScrollView!) -> NSAttributedString! { let str = "Tap the button below to add your first grokkleglob." let attrs = [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)] return NSAttributedString(string: str, attributes: attrs) } // Add your image func image(forEmptyDataSet _: UIScrollView!) -> UIImage! { return UIImage(named: "MYIMAGE") } // Add your button func buttonTitle(forEmptyDataSet _: UIScrollView!, for _: UIControlState) -> NSAttributedString! { let str = "Add Grokkleglob" let attrs = [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.callout), NSAttributedStringKey.foregroundColor: UIColor.white] return NSAttributedString(string: str, attributes: attrs) } // Add action for button func emptyDataSetDidTapButton(_: UIScrollView!) { let ac = UIAlertController(title: "Button tapped!", message: nil, preferredStyle: .alert) ac.addAction(UIAlertAction(title: "Hurray", style: .default, handler: nil)) present(ac, animated: true, completion: nil) }