web-dev-qa-db-ja.com

Swift 4タイプ '[String:AnyObject]?'の値を予期される引数タイプ '[NSAttributedStringKey:Any]?'に変換できません

Xcode 9に更新し、アプリをSwift 3からSwift 4.に変換しました。4.軸やその他の変数にラベルを付けるために文字列を使用するグラフがあります。つまり、moneyAxisString = "Money"ですが、以前は次のコードを使用してそれらを描くことができました。

moneyAxisString.draw(in: CGRect(x: CGFloat(coordinateXOriginLT + axisLength/3), y: CGFloat(coordinateYOriginRT + axisLength + 5 * unitDim), width: CGFloat(300 * unitDim), height: CGFloat(100 * unitDim)), withAttributes: attributes as? [String : AnyObject])

属性は次のように定義された辞書です

 attributes = [
        NSAttributedStringKey.foregroundColor: fieldColor,
        NSAttributedStringKey.font: fieldFont!,
        NSAttributedStringKey.paragraphStyle: style

    ]

これで、アプリがコンパイルされず、次のメッセージが表示されます。

タイプ「[String:AnyObject]?」の値を変換できませんか?期待される引数タイプ '[NSAttributedStringKey:Any]?'

5
Steve Harris

タイプの不一致です:[String : AnyObject]は明らかに[NSAttributedStringKey : Any]ではありません

⌥-clickNSAttributedStringKeyで宣言を確認します。


解決策はattributesを次のように宣言することです

var attributes = [NSAttributedStringKey : Any]()

ダウンキャストを削除するには

 ..., withAttributes: attributes)

簡単に書く

attributes = [.foregroundColor: fieldColor,
              .font: fieldFont!,
              .paragraphStyle: style]
11
vadian

NSAttributedStringKeyは、Swift 4.で構造体に変更されました。ただし、を使用する他のオブジェクトNSAttributedStringKeyは明らかに同時に更新されません。

他のコードを変更せずに最も簡単な修正append.rawValue toallです。 NSAttributedStringKeyセッターの出現-キー名をStringsに変換します。

let attributes = [
    NSAttributedStringKey.font.rawValue:  UIFont(name: "Helvetica-Bold", size: 15.0)!,
    NSAttributedStringKey.foregroundColor.rawValue: UIColor.white
] as [String : Any]

asでの!も今は必要ないことに注意してください。

または、配列を[String : Any]として前に宣言することで、最後にasキャストをスキップできます。

let attributes: [String : Any] = [
    NSAttributedStringKey.font.rawValue:  UIFont(name: "Helvetica-Bold", size: 15.0)!,
    NSAttributedStringKey.foregroundColor.rawValue: UIColor.white
]

もちろん、設定するNSAttributedStringKeyアイテムごとに.rawValueを追加する必要があります。

3
leanne

これを試して:

class func getCustomStringStyle() -> [NSAttributedStringKey: Any]
    {
        return [
            NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): UIFont.systemFont(ofSize: 16), // or your fieldFont
            NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.black, // or your fieldColor
            NSAttributedStringKey(rawValue: NSAttributedStringKey.paragraphStyle.rawValue): NSParagraphStyle.default // or your style
        ]
    }

または:

class func getCustomStringStyle() -> [String: Any]
    {
        return [
            NSAttributedStringKey.font.rawValue: UIFont.systemFont(ofSize: 16),
            NSAttributedStringKey.foregroundColor.rawValue: UIColor.black,
            NSAttributedStringKey.paragraphStyle.rawValue:NSParagraphStyle.default
        ]
    }
2

Swift 4.2
user_Dennisの例に基づいて構築

 func getCustomStringStyle() -> [NSAttributedString.Key: Any]
    {
        return [
            NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue): UIFont.systemFont(ofSize: 25), // or your fieldFont
            NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.black, // or your fieldColor
            NSAttributedString.Key(rawValue: NSAttributedString.Key.paragraphStyle.rawValue): NSParagraphStyle.default // or your style
        ]
    }
0
bibscy