Reputation: 957
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!
Upvotes: 42
Views: 9151
Reputation: 898
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.
Upvotes: 9
Reputation: 535191
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.
Upvotes: 116
Reputation: 20158
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
}
Upvotes: 1