func initializePickerViewProperties() {
    let font = UIFont (name: "SanFranciscoDisplay-Regular", size: 30.0)
    let highlightedFont = UIFont (name: "SanFranciscoDisplay-Bold", size: 35.0)
    pickerView.font = font!
    pickerView.highlightedFont = highlightedFont!
}

fairly simple, the pickerView in question is an AKPickerView

If I remove the forced unwrapping I get a compiler error. "Value of optional type UIFont not unwrapped, did you mean to use "!" or "?"?"

However, when I force unwrap it, I get a runtime error. "fatal error: unexpectedly found nil while unwrapping an Optional value"

3

There are 3 answers

2
konrad.bajtyngier On BEST ANSWER

Means your fonts are not initialized properly and give nil. You should safely unwrap them:

func initializePickerViewProperties() {
    if let font = UIFont (name: "SanFranciscoDisplay-Regular", size: 30.0),
        let highlightedFont = UIFont (name: "SanFranciscoDisplay-Bold", size: 35.0) {
        pickerView.font = font
        pickerView.highlightedFont = highlightedFont
    }
}
1
TPlet On

Try printing all available fonts, and check the spelling of your fontname

 for fontfamily in UIFont.familyNames{
        for fontname in UIFont.fontNames(forFamilyName: fontfamily){
            print(fontname)
        }
    }
0
Kaye On

You must first add your custom font inside your Info.plist.

To look for the Info.plist file:

  1. Make sure you have imported your custom font onto your project.
  2. Click the blue icon on the topmost part of your project navigator--the project itself.
  3. Go to the Info tab.
  4. Look for Fonts provided by application.
  5. Then, click the plus icon to add a new key-value pair to our Info.plist.
  6. Add the exact file name of the font you imported, including the file extension.

I solved mine using this. Hope it works for you as well.