Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find an overload for “init” that accepts the supplied arguments SWIFT

Tags:

xcode

ios

swift

I used this code

    self.navigationController?.navigationBar.titleTextAttributes =
        [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 20),
        NSForegroundColorAttributeName: UIColor.whiteColor()]

and I'm getting error "Could not find an overload for “init” that accepts the supplied arguments"

like image 984
patrikbelis Avatar asked Oct 22 '14 16:10

patrikbelis


1 Answers

UIFont(name:size:) is now a failable initializer -- it will return nil if it can't find that font and crash your app if you unwrap the return value. Use this code to safely get the font and use it:

if let font = UIFont(name: "HelveticaNeue-Light", size: 20) {
    self.navigationController?.navigationBar.titleTextAttributes = 
            [NSFontAttributeName: font, 
             NSForegroundColorAttributeName: UIColor.whiteColor()]
}
like image 131
Nate Cook Avatar answered Sep 23 '22 21:09

Nate Cook