【问题标题】:NSAttributedStringKey giving an unresolved identifier errorNSAttributedStringKey 给出未解决的标识符错误
【发布时间】:2017-06-20 00:27:04
【问题描述】:

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

这是导致错误的代码行:

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

【问题讨论】:

  • 这是一个 iOS 11 API。
  • 您看到的错误是什么?另外,您使用的是什么版本的 Swift/Xcode?​​span>
  • @chrismanderson 错误是“使用未声明的类型 NSAttributedStringKey”Xcode 版本 - 8.3.3。我认为这与版本问题有关,但不确定是否有解决方法等。
  • 使用的 Xcode9:[NSForegroundColorAttributeName: color]

标签: ios swift xcode


【解决方案1】:

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

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

如此处所述NSFontAttributeName vs NSAttributedStringKey.font

需要确认是否可以在低于ios11的版本上运行

【讨论】:

  • 问题与Swift版本无关。 API 更改是操作系统版本更改的结果。这一变化发生在 iOS 11、macOS 10.13、tvOS 11.0 和 watchOS 4.0 中。
【解决方案2】:

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

同时,试试这个。

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

这应该可行。

【讨论】:

    【解决方案3】:

    您尝试使用的代码是 iOS 11 中添加的新 API。由于您使用的是 Xcode 8,因此您没有使用 iOS 11。因此您需要使用当前的(非测试版)API。

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

    【讨论】:

    • 谢谢!设法摆脱了错误。但是,按照教程,我没有收到显示的输出。我假设本教程可能无法使用我的版本?教程 - raywenderlich.com/153591/…
    • 你应该找到不是基于新测试版和 iOS 11 的教程。
    【解决方案4】:

    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
    

    你好将是红色的。

    【讨论】:

    • 该问题与 Swift 或 Xcode 的版本无关。 API 更改是操作系统版本更改的结果。这一变化发生在 iOS 11、macOS 10.13、tvOS 11.0 和 watchOS 4.0 中。
    【解决方案5】:

    此示例仅适用于 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])  
        }    
    }
    

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        猜你喜欢
        • 2016-10-08
        • 1970-01-01
        • 1970-01-01
        • 2019-05-01
        • 2018-12-05
        • 2016-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多