swift 2.2: failable initializers in lazy properties
Asked Answered
C

1

13

First very appreciate for your help. I just upgraded Xcode yesterday which contains swift 2.2. I've faced a few issues but I fixed them quickly by following the "what's new in swift 2.2" topics from Natashatherobot. But there is one issue I cannot fix. It's about failable initializers of UIFont which was introduced in swift 2.2. Attached is a simple piece of code that will report error in swift 2.2. It might not report the error immediately, until I cleaned the project.

lazy var somelabel: UILabel = {

        let label = UILabel()
        let font = UIFont(name: "somefont", size: 10) ?? UIFont.systemFontOfSize(10) //this line gave me error
        label.font = font
        label.text = "Calculating..."

        return label
    }()

Here is the screenshot of the error

enter image description here

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

I can fix it in two ways:

Method 1: don't put this line: let font = UIFont(name: "somefont", size: 10) ?? UIFont.systemFontOfSize(10) in the 'lazy instantiation' closure. (Put it in computed properties reports no error)

Method 2: instead of using:

 UIFont(name: "somefont", size: 10)

use the below instead( However I don't think this should be the right approach because it makes the initializer more "objc" style):

UIFont.init(name: "somefont", size: 10)

But I still don't understand why it would report me error in the lazy property closure. I will be very appreciated if someone can give me some explanations.

Capp answered 23/3, 2016 at 3:54 Comment(5)
Really? Putting in the explicit init makes this work? You're a genius! You should just give that as an answer. This is a serious bug and you've already found a workaround.Rosetterosewall
Oh, and if you can reproduce the problem, please file a bug report with Apple. (I can't reproduce it, even though it seems to be popping up a lot.)Rosetterosewall
Thanks @matt, I think you definitely can replicate the issue by using the piece of code I attached if you cleaned your project. I'm not sure whether this is a bug or default behavior for lazy properties in swift 2.2 yet. At the same time, if this happened only on my xcode, I will try to do a clean installation insteadCapp
I am unable to replicate this. However, I experienced a similar problem with the operator ?? (can't remember what it was) and I had to literally use an if let or guard and then it all worked. There may be a bug. But in this particular case, your code compiles fine on my machine, even after a full cleanup.Holmes
@MartínMarconcini , thanks a lot for your help. My project was set up before swift 2.2. It's strange that I cannot replicate the issue after starting up a new project. Maybe it was a bug. Anyone who is facing such issue can try to start a new project in swift 2.2 environment.Capp
C
2

This might be a bug of the latest version xcode. Those whose project was set up before the upgrade might face this problem. Anyone who is luck enough to face such issue can try to start a new project in swift 2.2 environment.

Capp answered 23/3, 2016 at 8:18 Comment(8)
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From ReviewStallfeed
Please don't add "thank you" as an answer. Instead, accept the answer that you found most helpful. - From ReviewLifesize
@Lifesize . Ok I see. Since this issue might be a bug of xcode, I will edit my solution a bit.Capp
@Stallfeed Thanks I will.Capp
For the record, I also tried it in a project that it was created in Xcode 7.1< and it still works. But good to hear that you solved your problem. Have you tried the "three degrees" of cleanup? That usually fixes all the problems. Number one is cleanup. Then you do a cleanup build folder (cmd+option+ctrl+shift+K) and finally delete derived data from Xcode. Then close and reopen Xcode. That usually does it for me.Holmes
sorry, but this is the worst accepted answer ever. Thanks @MartinMarconcini still not working... :(Parliament
Using UIFont.init fixed the issueParliament
@TomCobo, r u facing the same issue that I had before?Capp

© 2022 - 2024 — McMap. All rights reserved.