2016-12-03 47 views
1

當我將Uber SDK集成到我的項目中時,我在swift 2.3中遇到了這個錯誤,但我不知道要解決這個問題。條件綁定的iOS初始化程序必須具有可選類型,而不是'CGFont'?

class FontUtil { 
static func loadFontWithName(name: String, familyName: String) -> Bool { 
    if let path = NSBundle(forClass: FontUtil.self).pathForResource(name, ofType: "otf") { 
     if let inData = NSData(contentsOfFile: path) { 
      var error: Unmanaged<CFError>? 
      let cfdata = CFDataCreate(nil, UnsafePointer<UInt8>(inData.bytes), inData.length) 
      if let provider = CGDataProviderCreateWithCFData(cfdata) { 
       if let font = CGFontCreateWithDataProvider(provider) ---> Error found in this line { 
        if (CTFontManagerRegisterGraphicsFont(font, &error)) { 
         return true 
        } 
        print("Failed to load font with error: \(error)") 
       } 
      } 
     } 
    } 
    return false 
}} 
+1

解決方法是在您放入標題的錯誤消息中給出的。 :)提示:在之前的Swift版本中返回一個Optional的東西現在不再返回Optional了... – Moritz

回答

1

正如埃裏克說,該CGFontCreateWithDataProvider功能不再返回一個Optional值,這樣你就不會需要包裝這一說法,並在if可選綁定的後續調用。您的代碼將更改爲:

let font = CGFontCreateWithDataProvider(provider) 
if (CTFontManagerRegisterGraphicsFont(font, &error)) { 
    return true 
} 
print("Failed to load font with error: \(error)") 
+0

感謝您的回答。@kgaleman –

相關問題