2014-09-22 12 views
8

Nach 7 bis Xcode 6.1 Beta 2 von Xcode 6 Beta aktualisieren, funktioniert die folgende nicht mehr:Xcode 6.1 Attribut Wörterbücher in Swift

let font = UIFont(name: "Arial", size: 16) 
let colour = UIColor.redColor() 
let attributes = [NSFontAttributeName: font, NSForegroundColorAttributeName: colour] 

Ich habe speziell versucht das Wörterbuch als

erklärt
let attributes: [NSString : AnyObject] = [NSFontAttributeName: font, NSForegroundColorAttributeName: colour] 

aber ich erhalte den Fehler "Can not convert ... 'Dictionary' in 'NSString!'". Die Deklaration des Schlüssels NSString! statt NSString beklagt, dass NSString! nicht hashbar ist. Irgendwelche Hinweise?

+0

für Xcode6.01 zum Spielplatz kopiert - es –

+0

funktioniert der Code versagt in XCode 6.1 Beta 2. –

+1

@MaximShoustin bestätigen - So Warum der Downvote? – Grimxn

Antwort

18

Sortiert. Wie üblich ist der tatsächliche Fehler ein Ablenkungsmanöver. UIFont(name: , size:) hat jetzt einen init? Initialisierer, und so ist optional. Gemäßer Gebrauch ist jetzt:

let font = UIFont(name: "Arial", size: 16)! // Unwrapped 
let colour = UIColor.redColor() 
let attributes: [NSString : AnyObject] = [NSFontAttributeName: font, NSForegroundColorAttributeName: colour] 

oder richtiger:

if let font = UIFont(name: "Arial", size: 16) { 
    let colour = UIColor.redColor() 
    let attributes: [NSString : AnyObject] = [NSFontAttributeName: font, NSForegroundColorAttributeName: colour] 
    // ... 
} 
+0

hatte genau dieses Problem. Ich habe ein paar verschiedene Modifikationen ausprobiert (die Parameter des Wörterbuchtyps angeben, stattdessen 'NSDictionary' verwenden), und alle Compilerfehler haben mich in die völlig falsche Richtung gebracht. Vielen Dank! –

Verwandte Themen