Ios NSAttributedStringKey提供未解析的标识符错误
我正在按照一个在线教程构建一个杂志类型的iOS应用程序。我正在尝试使用NSAttributedStringKey,但不断出现如下所示的错误。有什么想法吗 这是导致错误的代码行:Ios NSAttributedStringKey提供未解析的标识符错误,ios,swift,xcode,Ios,Swift,Xcode,我正在按照一个在线教程构建一个杂志类型的iOS应用程序。我正在尝试使用NSAttributedStringKey,但不断出现如下所示的错误。有什么想法吗 这是导致错误的代码行: let attrs = [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: font] as [NSAttributedStringKey : Any] 您尝试使用的代码是IOS11中添加的新API。因为您使用的是Xcod
let attrs = [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: font] as [NSAttributedStringKey : Any]
您尝试使用的代码是IOS11中添加的新API。因为您使用的是Xcode 8,所以您没有使用iOS 11。因此,您需要使用当前的(非测试版)API
您正在尝试在iOS 11之前的版本(可能是iOS 10)上使用iOS 11 API。惊讶的是,你发现一个教程已经使用测试版功能 同时,试试这个
let attrs=[NSForegroundColorAttributeName:color,NSFontAttributeName:font]
这应该行得通。这个例子只适用于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 在Swift 4中,NSFontAttributeName已替换为NSAttributedStringKey.font 如前所述
需要确认它是否能在低于ios11的版本上工作,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
Hello将以红色显示。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)
}
这是iOS11API。您看到的错误是什么?另外,您使用的是哪个版本的Swift/Xcode?@chrismanderson错误是“使用未声明的NSAttributedStringKey类型”Xcode版本-8.3.3。我认为这与版本问题有关,但不确定是否有解决方法等。Xcode9使用:[NSForegroundColorAttributeName:color]谢谢!这成功地消除了错误。但是,在教程之后,我没有收到显示的输出。我假设教程可能无法使用我的版本?教程-你应该找到一个不是基于新测试版和iOS 11的教程。这个问题与Swift版本无关。API更改是操作系统版本更改的结果。这一变化出现在iOS 11、macOS 10.13、tvOS 11.0和watchOS 4.0中。这一问题与Swift或Xcode的版本无关。API更改是操作系统版本更改的结果。iOS 11、macOS 10.13、tvOS 11.0和watchOS 4.0都发生了变化。
// 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)
}