1

私のプロジェクトでは以下のコードを使用しています。速攻4へのアップデート後、私はエラーを受けています。どうすれば修正できますか?'NSAttributedStringKey'タイプのインデックスを持つ '[String:AnyObject]'の値をサブスクリプトできません

コード:

let returnString : NSAttributedString 

    if styleList.count > 0 
    { 
     var attrs = [String:AnyObject]() 
     attrs[NSAttributedStringKey.font] = codeFont 
     for style in styleList 
     { 
     if let themeStyle = themeDict[style] 
     { 
      for (attrName, attrValue) in themeStyle 
      { 
       attrs.updateValue(attrValue, forKey: attrName) 
      } 
     } 
    } 

    returnString = NSAttributedString(string: string, attributes:attrs) 
} 

ここあるエラー:


型の値を添字できません '[文字列:ANYOBJECT]' 'NSAttributedStringKey'

タイプのインデックスを持ちます

'[String:AnyObject]'の値を期待される引数型 '[NSAttributedStringKey:Any]に変換できませんか?'

+2

属性がタイプ[NSAttributedStringKey:任意]のものでなければならない、ではありません[文字列:AnyObject]。 –

+0

説明を与える: 'NSAttributedString'は思うように' NSString'のサブクラスではありません。 –

+0

@RamyAlZuhouri細かい作業、ありがとう –

答えて

2

swift 4 - NSAttributedString表現が完全に変更されました。

これを試してみてください[NSAttributedStringKey:Any]

であなたの属性辞書attrsタイプ[String:AnyObject]を交換してください:ここで

let returnString : NSAttributedString 

    if styleList.count > 0 
    { 
     var attrs = [NSAttributedStringKey:Any]() // Updated line 
     attrs[NSAttributedStringKey.font] = codeFont 
     for style in styleList 
     { 
     if let themeStyle = themeDict[style] 
     { 
      for (attrName, attrValue) in themeStyle 
      { 
       attrs.updateValue(attrValue, forKey: attrName) 
      } 
     } 
    } 

    returnString = NSAttributedString(string: string, attributes:attrs) 
} 

は、Appleからノートです:NSAttributedString - Creating an NSAttributedString Object

+1

ありがとう...それは私のために働いた... – Aayushi

関連する問題