Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift 2.2: failable initializers in lazy properties

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.

like image 490
fans3210 Avatar asked Mar 23 '16 03:03

fans3210


People also ask

What are Failable Initializers in Swift?

In a failable initializer, return nil indicates that initialization has failed; no other value can be returned. In the example, failure occurs when the string could not be parsed as an integer. Otherwise, self is initialized to the parsed value.

What is failable initializer?

It makes sense sometimes not to return an object if the initialization of one or more properties that play crucial role in the type does not succeed. A failable initializer in Swift is an initializer that allows to do that, as it can potentially return nil if one or more conditions are not satisfied.

What is failable initializers in Swift?

Swift version 1.1 is new in Xcode 6.1, and it introduces a new feature: failable initializers. Initialization is the process of providing initial values to each of the stored properties of a class or struct, establishing the invariants of the object.

What is a lazy property in Swift?

Lazy properties allow you to create certain parts of a Swift type when needed, rather than doing it as part of its initialization process.

What is lazy initialization in Swift programming?

In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. That little quote pretty much sums up everything, however because we're working with the Swift programming language, we have a thing called optionals.

How do I report an object initialization failure in Swift?

With Swift version 1.1, such failures can be reported using a failable initializer. When constructing an object using a failable initializer, the result is an optional that either contains the object (when the initialization succeeded) or contains nil (when the initialization failed).


1 Answers

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.

like image 121
fans3210 Avatar answered Oct 19 '22 20:10

fans3210