2017-06-20 5 views
4

私はオンラインチュートリアルに従って、雑誌タイプのiOSアプリケーションを作成しています。 NSAttributedStringKeyを使用しようとしていますが、以下に示すエラーが発生し続けます。何か案は?未解決の識別子エラーを返すNSAttributedStringKey

これは、エラーの原因となっているコードの行です:あなたはiOSのを使用していないのXcode 8を使用しているので、あなたが使用しようとしている

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

に動作しますiOSの11 APIだこと。 – rmaddy

+0

あなたが見ているエラーは何ですか?また、Swift/Xcodeのどのバージョンを使用していますか? – chrismanderson

+0

@chrismandersonエラーは、 "宣言されていない型のNSAttributedStringKeyを使用する" Xcodeバージョン - 8.3.3です。私はそれはバージョンの問題とは関係があると思うが、その周りに道があるかどうかわからない。 – Johnm95

答えて

3

コードは、iOS 11に追加された新しいAPIであります11.したがって、現在の(ベータ以外の)APIを使用する必要があります。

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

ありがとう!それはそのエラーを取り除くことができました。しかし、チュートリアルに続いて、出力が表示されません。私はチュートリアルがおそらく私のバージョンで動作できないと仮定していますか?チュートリアル - https://www.raywenderlich.com/153591/core-text-tutorial-ios-making-magazine-app – Johnm95

+0

新しいベータ版とiOS 11に基づいていないチュートリアルがあります。 – rmaddy

16

iOS 11以前のバージョン(おそらくiOS 10)でiOS 11 APIを使用しようとしています。すでにベータ版の機能を使ったチュートリアルがあることに驚きました。

これまでのところ、これを試してください。

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

、それが動作するはずです。

3

この例では、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]) 
    }  
} 
0

プロジェクトはおそらく異なるバージョンのSwiftです。

Swift 4では、NSFontAttributeNameがNSAttributedStringKey.fontに置き換えられました。

ここNSFontAttributeName vs NSAttributedStringKey.font

述べたように確認する必要があり、それはバージョン未満ios11

関連する問題