2017-02-04 21 views
-2
let darkGray = UIColor.darkGray 
    let lightGray = UIColor.lightGray 
    let white = UIColor.white 
    // Navigation bar background. 
    UINavigationBar.appearance().barTintColor = darkGray 
    UINavigationBar.appearance().tintColor = lightGray 
    // Color of typed text in the search bar. 
    let searchBarTextAttributes: [AnyHashable: Any] = [NSForegroundColorAttributeName: lightGray, NSFontAttributeName: UIFont.systemFont(ofSize: CGFloat(UIFont.systemFontSize))] 
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes as! [String : Any] 
    // Color of the placeholder text in the search bar prior to text entry. 
    let placeholderAttributes: [AnyHashable: Any] = [NSForegroundColorAttributeName: white, NSFontAttributeName: UIFont.systemFont(ofSize: CGFloat(UIFont.systemFontSize))] 
    // Color of the default search text. 
    // NOTE: In a production scenario, "Search" would be a localized string. 
    let attributedPlaceholder = NSAttributedString(string: "Search", attributes: placeholderAttributes) 
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = attributedPlaceholder 

let attributedPlaceholderでは、誰かが私のためにこの問題を解決できるかどうか疑問に思いますか?あなたはこのような場合には、[String:Any?]String、または[AnyHashable:Any?]AnyHashableを変換することはできませんのでは[anyhashable:any]型の値を[string:any]に変換できません

let placeholderAttributes = ["NSForegroundColorAttributeName": UIColor.white, "NSFontAttributeName": UIFont.systemFont(ofSize: CGFloat(UIFont.systemFontSize))] 
// equivalent of: let placeholderAttributes: [String: Any?] = ... 
let attributedPlaceholder = NSAttributedString(string: "Search", attributes: placeholderAttributes) 

+1

エラーはかなり明確です。あなたは '[Any:String:Any]'に渡そうとしている '[AnyHashable:Any]'を持っています。 'placeholderAttributes'に' [String:Any] 'として注釈を付けてください。 – Hamish

+0

ああ、ありがとう、今の意味がある! – JammyBugger

答えて

1
let placeholderAttributes: [AnyHashable: Any] = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont.systemFont(ofSize: CGFloat(UIFont.systemFontSize))] 
    // Color of the default search text. 
    // NOTE: In a production scenario, "Search" would be a localized string. 
    let attributedPlaceholder = NSAttributedString(string: "Lookup", attributes: placeholderAttributes as? [String: Any]) 

これは私のために働いた、ありがとう。

0

は、これであなたのコードを置き換えることができます。

関連する問題