//
//  FontLoader.swift
//  SwiftIconFont
//
//  Created by Sedat Ciftci on 18/03/16.
//  Copyright © 2016 Sedat Gokbek Ciftci. All rights reserved.
//

import UIKit
import Foundation
import CoreText

class FontLoader: NSObject {
    class func loadFont(_ fontName: String) {
        
        let bundle = Bundle(for: FontLoader.self)
        var fontURL = URL(string: "")
        for filePath : String in bundle.paths(forResourcesOfType: "ttf", inDirectory: nil) {
            let filename = NSURL(fileURLWithPath: filePath).lastPathComponent!
            if filename.lowercased().range(of: fontName.lowercased()) != nil {
                fontURL = NSURL(fileURLWithPath: filePath) as URL
            }
        }
        
        do
        {
            let data = try Data(contentsOf: fontURL!)
            
            let provider = CGDataProvider(data: data as CFData)
            let font = CGFont.init(provider!)
            
            var error: Unmanaged<CFError>?
            if !CTFontManagerRegisterGraphicsFont(font, &error) {
                let errorDescription: CFString = CFErrorCopyDescription(error!.takeUnretainedValue())
                let nsError = error!.takeUnretainedValue() as AnyObject as! NSError
                NSException(name: NSExceptionName.internalInconsistencyException, reason: errorDescription as String, userInfo: [NSUnderlyingErrorKey: nsError]).raise()
            }
            
        } catch {
            
        }
        
        
    }
}

//
//  FontLoader.swift
//  SwiftIconFont
//
//  Created by Sedat Ciftci on 18/03/16.
//  Copyright © 2016 Sedat Gokbek Ciftci. All rights reserved.
//

import UIKit
import Foundation
import CoreText

class FontLoader: NSObject {
    
    class func loadFont(_ fontName: String) {
        
        let bundle = Bundle(for: FontLoader.self)
        let paths = bundle.paths(forResourcesOfType: "ttf", inDirectory: nil)
        var fontURL = URL(string: "")
        var error: Unmanaged<CFError>?
        
        paths.forEach() {
            guard let filename = NSURL(fileURLWithPath: $0).lastPathComponent,
                let _ = filename.lowercased().range(of: fontName.lowercased()) else {
                    return
            }
            
            fontURL = NSURL(fileURLWithPath: $0) as URL
        }
        
        guard let unwrappedFontURL = fontURL,
            let data = try? Data(contentsOf: unwrappedFontURL),
            let provider = CGDataProvider(data: data as CFData),
            let font = CGFont.init(provider) else {
                
                return
        }
        
        guard CTFontManagerRegisterGraphicsFont(font, &error) else {
            
            return
        }
        
        let errorDescription: CFString = CFErrorCopyDescription(error!.takeUnretainedValue())
        let nsError = error!.takeUnretainedValue() as AnyObject as! NSError
        NSException(name: NSExceptionName.internalInconsistencyException, reason: errorDescription as String, userInfo: [NSUnderlyingErrorKey: nsError]).raise()
    }
}

Fix build error on XCode 9. Minor refactoring to improve type safety
Fix build error on XCode 9. Minor refactoring to improve type safety
