Swift 4 Conversion error-NSAttributedStringKey: Any
j'ai converti mon application récemment et je reçois l'erreur
"Impossible de convertir une valeur de type '[Chaîne : Toute]' à attendre argument de type '[NSAttributedStringKey: Aucune?'
barButtonItem.setTitleTextAttributes(attributes, for: .normal)
Code entier:
class func getBarButtonItem(title:String) -> UIBarButtonItem {
let barButtonItem = UIBarButtonItem.init(title: title, style: .plain, target: nil, action: nil)
let attributes = [NSAttributedStringKey.font.rawValue: UIFont(name: "Helvetica-Bold", size: 15.0)!, NSAttributedStringKey.foregroundColor: UIColor.white] as! [String : Any]
barButtonItem.setTitleTextAttributes(attributes, for: .normal)
return barButtonItem
}
4 réponses
pourquoi vous avez cette erreur
Précédemment, votre attributes est définie comme [String: Any], d'où vient la clé NSAttributedStringKey comme une chaîne.
pendant la migration, le compilateur essaie de garder le [String: Any] type. Cependant, NSAttributedStringKey devient une structure dans swift 4. Ainsi, le compilateur essaie de changer de chaîne en obtenant sa valeur brute.
Dans ce cas, setTitleTextAttributes cherche [NSAttributedStringKey: Any] mais vous avez fourni [String: Any]
pour corriger ceci erreur:
Supprimer .rawValue et tu attributes[NSAttributedStringKey: Any]
à savoir, modifier cette ligne
let attributes = [NSAttributedStringKey.font.rawValue:
UIFont(name: "Helvetica-Bold", size: 15.0)!,
NSAttributedStringKey.foregroundColor: UIColor.white] as! [String : Any]
let attributes = [NSAttributedStringKey.font:
UIFont(name: "Helvetica-Bold", size: 15.0)!,
NSAttributedStringKey.foregroundColor: UIColor.white] as! [NSAttributedStringKey: Any]
Sa attend NSAttributedStringKey (NSAttributedStringKey.font) et vous envoyez String (NSAttributedStringKey.font.rawValue).
veuillez donc remplacer NSAttributedStringKey.font.rawValueNSAttributedStringKey.font comme ci-dessous :
let attributes = [NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15.0)!, NSAttributedStringKey.foregroundColor: UIColor.white]
Comme indiqué dans les précédentes réponses, NSAttributedStringKey a été changée en une structure dans Swift 4. Cependant, d'autres objets qui utiliserNSAttributedStringKey n'a apparemment pas été mis à jour en même temps.
la solution la plus simple, sans avoir à changer l'un de vos autres codes, est append.rawValue vos occurrences de NSAttributedStringKey setters - tournez la clé de noms dans l' String s:
let attributes = [
NSAttributedStringKey.font.rawValue: UIFont(name: "Helvetica-Bold", size: 15.0)!,
NSAttributedStringKey.foregroundColor.rawValue: UIColor.white
] as [String : Any]
Notez que vous n'aurez pas besoin d' ! à l' as maintenant, soit.
alternativement, vous pouvez sauter le as lancer à la fin en déclarant que le tableau est [String : Any] d'avance:
let attributes: [String : Any] = [
NSAttributedStringKey.font.rawValue: UIFont(name: "Helvetica-Bold", size: 15.0)!,
NSAttributedStringKey.foregroundColor.rawValue: UIColor.white
]
bien sûr, vous avez encore besoin d'ajouter la ligne .rawValue pour chaque NSAttributedStringKey article que vous avez réglé.
la réponse de leanne est correct pour le cas où vous avez encore besoin d'utiliser [String : Any] et non [NSAttributedStringKey : Any].
Par exemple, dans UIKit UITextView.typingAttributes est de type [String : Any]. Donc pour cette propriété, vous devez utiliser convertis attributs (personnalisée):
let customAttributeName = "MyCustomAttributeName"
let customValue: CGFloat = 15.0
let normalTextAttributes: [NSAttributedStringKey : Any] =
[NSAttributedStringKey.font : UIFont.systemFont(ofSize: 14.0),
NSAttributedStringKey.foregroundColor : UIColor.blue,
NSAttributedStringKey.backgroundColor : UIColor.clear,
NSAttributedStringKey(rawValue: customAttributeName): customValue]
textView.typingAttributes = normalTextAttributes.toTypingAttributes()
où toTypingAttributes() est une fonction définie par l'extension de vos fichiers de projet:
extension Dictionary where Key == NSAttributedStringKey {
func toTypingAttributes() -> [String: Any] {
var convertedDictionary = [String: Any]()
for (key, value) in self {
convertedDictionary[key.rawValue] = value
}
return convertedDictionary
}