2017-10-25 5 views
3

여기서 사용자 정의 글꼴을 초기화하려하지만 오류를 표시하려고합니다.swift 4 : '[UIFontDesciptor.AttributeName : Any]'유형의 색인에 'String'유형의 색인을 붙일 수 없습니다.

extension UIFont { 
@objc convenience init(myCoder aDecoder: NSCoder) { 
    if let fontDescriptor = aDecoder.decodeObject(forKey: "UIFontDescriptor") as? UIFontDescriptor { 
     if let fontAttribute = fontDescriptor.fontAttributes["NSCTFontUIUsageAttribute"] as? String { // HERE SHOWING THE ERROR 
      var fontName = "" 
      switch fontAttribute { 
      case "CTFontRegularUsage": 
       fontName = appFontName 
      case "CTFontEmphasizedUsage", "CTFontBoldUsage": 
       fontName = appFontBoldName 
      case "CTFontObliqueUsage": 
       fontName = appFontItalicName 
      default: 
       fontName = appFontName 
      } 
      self.init(name: fontName, size: fontDescriptor.pointSize)! 
     } 
     else { 
      self.init(myCoder: aDecoder) 
     } 
    } 
    else { 
     self.init(myCoder: aDecoder) 
    } 
} 
... 
} 

이것은 swift-3.2에서 완벽하게 작동합니다. 그러나 신속한 4.0으로 실행되지 않습니다. 도와주세요. 미리 감사드립니다.

답변

5

글꼴 속성 식별자의 유형이 String에서 UIFontDescriptor.AttributeName으로 변경되었습니다.

장점은 그럼 당신은

if let fontAttribute = fontDescriptor.fontAttributes[.nsctFontUIUsage] as? String { 

를 작성하거나

if let fontAttribute = fontDescriptor.fontAttributes[UIFontDescriptor.AttributeName(rawValue: "NSCTFontUIUsageAttribute")] as? String { 
+0

감사 @vadian 확장자가없는 수는 확장을

extension UIFontDescriptor.AttributeName { static let nsctFontUIUsage = UIFontDescriptor.AttributeName(rawValue: "NSCTFontUIUsageAttribute") } 

을 선언 할 수 있습니다, 그것은 나를 위해 작동합니다! –

관련 문제