41

So I had updated my XCode to 7.3 today evening.

In one of my projects, I get the following error for few labels where I set the font:

'(name: String, size: CGFloat) -> UIFont' is not convertible to '(name: String, size: CGFloat) -> UIFont?'

EDIT: This is my code for Title View in Navigation Bar:

let aTitleFrame: CGRect = CGRectMake(0, aHeaderTitleSubtitleView.frame.midY / 2, 200, 24)
let aTitleView: UILabel = UILabel(frame: aTitleFrame)
aTitleView.backgroundColor = UIColor.clearColor()
aTitleView.font = UIFont(name: "Roboto-Regular", size: 15) // ERROR POPS UP HERE
aTitleView.textAlignment = NSTextAlignment.Center
aTitleView.textColor = UIColor.whiteColor()

This is my code for an Attributed String for a UILabel:

let aAttributedFundLabel: NSMutableAttributedString = NSMutableAttributedString(string: "Raising\n$ \(fund)")
aAttributedFundLabel.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGrayColor(), range: NSRange(location: 0, length: 7))
aAttributedFundLabel.addAttribute(NSFontAttributeName, value: UIFont(name: "Roboto-Regular", size: 15)!, range: NSRange(location: 0, length: 7)) // ERROR POPS UP HERE 
aAttributedFundLabel.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: NSRange(location: 8, length: fund.characters.count + 2))
aAttributedFundLabel.addAttribute(NSFontAttributeName, value: UIFont(name: "Roboto-Regular", size: 16)!, range: NSRange(location: 8, length: fund.characters.count + 2)) // ERROR POPS UP HERE
startupFund.attributedText = aAttributedFundLabel

This happens only in two files in my entire project.

I opened up another project, but I was able to build and run it without any errors, even though I do set the font for multiple labels there as well.

Any idea why this is happening?

TIA!

Vishal
  • 887
  • 9
  • 22

3 Answers3

116

Elsewhere on SO, someone suggest that where you have this:

aTitleView.font = UIFont(name: "Roboto-Regular", size: 15)

...you should try writing this:

aTitleView.font = UIFont.init(name: "Roboto-Regular", size: 15)

I can take no credit for this (because I can't reproduce the bug) so I'm just guessing! But it would be very interesting to know if it actually works.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Wow, that's amazing. What really gets me is that _I can't reproduce this!_ I am dying to experience this problem for myself. Please, please, file a bug with Apple if you can _possibly_ make a reproducible example. – matt Mar 23 '16 at 04:14
  • Will definitely do that matt! :D – Vishal Mar 23 '16 at 04:15
  • 3
    Had the same issue, I'm setting this inside a lazy var instantiation; doing it outside works without the init. – Arnaud Mar 23 '16 at 09:57
  • This didn't fix the problem for me. Sticking to the UIFontDescriptor approach for now. – zumzum Mar 24 '16 at 14:58
  • @zumzum I just wish I could even _have_ this problem! I'm totally unable to reproduce it. – matt Mar 24 '16 at 14:59
  • ha, this one is a bizarre one, I saw a radar posted to the Xcode team... go figure – zumzum Mar 25 '16 at 02:15
  • Worked for me, double upvote if I could. Hate fighting the compiler in these cases. Xcode 7.3 stable (7D175) / El Capitan 11.4 – BaseZen Apr 05 '16 at 02:04
  • Thanks +matt, I'm not really surprise if you can't reproduce this bug, because I have 2 files with this code and the error just appeared in one of them.. really weird.. – Bobby Stenly Apr 09 '16 at 17:13
  • this fixed the issue for me as well :) the code is taking place it a lazy var and the Xcode 7.3 started marking it with an error diagnostic at some point for no reason – Ilias Karim Apr 26 '16 at 00:48
  • Thanks, but strange!!, the same code is working in my one VC and throwing error on another VC. – Bista May 02 '16 at 11:26
  • Oh, Come on! If every objects are initializing without `.init` then why should this like this? – Mohammad Zaid Pathan Aug 01 '16 at 05:25
  • 1
    Just to add to the confusion, the Build target of my app was working forever, but when I went to Archive it for Release… I encountered this issue. Adding `.init(` resolved it. – cleverbit Sep 20 '16 at 22:28
  • thx for the fix! -- the issue repros only in archive mode for me – amok Oct 01 '16 at 04:42
  • Fixed it for me! Was only getting the issue when doing an archive. – bgolson Oct 17 '16 at 16:38
  • Fix working for me too, never had issue with this before i have no idea where this is coming from. – thibaut noah Dec 09 '16 at 16:08
  • This did it for me. Using Xcode 8.2 (8C38)... Why is this happening? – gaskbr Dec 14 '16 at 12:19
9

I had this problem as well. What fixed it for me was to turn off Whole Module Optimization.

Bulid Settings > Swift Compiler - Code Generation > Optimization Level

I had set it to Fastest (Whole Module Optimization). When I set it to None I didn't have this problem anymore. For context, this is a mixed code base with both Objective-C and Swift.

pietrorea
  • 841
  • 6
  • 14
  • 4
    Ditto. Same as me. I could only produce this error when archiving my project. Building+running in debug mode was error-free. So, of course, I looked at my debug/release optimizations and tested Fast(WMO) vs. Fast[-O]. That did the trick, but I want to keep WMO for performance reasons, so I'll be using `UIFont.init(name:` for the time being. – slider Jul 27 '16 at 07:40
  • `UIFont.init` also doesn't work for me, have to turn it to single file optimization, curse old code – Tj3n Oct 21 '16 at 05:10
  • Is there any option to use Fast(WMO) and use UIFont() together? – Asif Bilal Dec 09 '16 at 10:02
  • Xcode sucks, seriously, even disabling optimization it still happens. I "admired" Apple till I started to code with Xcode and I know a bunch of very different platforms – Jaime Agudo Dec 12 '16 at 19:01
1

I had the same issue and doing this allows me to compile again:

let descriptor = UIFontDescriptor(name: "OpenSans-Semibold", size: 10.0)
label.font = UIFont(descriptor: descriptor, size: 10.0)

So, use the UIFontDescriptor ...

Also doing this works for me:

if let font = UIFont(name: "OpenSans-Semibold", size: 10) {
    label.font = font
}
zumzum
  • 17,984
  • 26
  • 111
  • 172