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

신속한 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) 
} 

가 참고 인 애플 :하지 : NSAttributedString - Creating an NSAttributedString Object

+1

감사합니다 ... 그것은 나를 위해 일했습니다 ... – Aayushi

관련 문제