[swift-users] lazy initialisation

Mark Dalrymple markd at borkware.com
Mon Jul 4 12:42:50 CDT 2016


This works for me


class Blorg: NSObject, URLSessionDelegate {

    var config: URLSessionConfiguration



    lazy var session: URLSession = {

        print("howdy")

        return URLSession(configuration: self.config, delegate: self,
delegateQueue: nil)

    }()



    override init() {

        config = URLSessionConfiguration.default()

    }

}

Cheers,
++md


On Mon, Jul 4, 2016 at 1:04 PM, J.E. Schotsman via swift-users <
swift-users at swift.org> wrote:

> Hello,
>
> I need to initialize a variable of a class with a closure using the value
> of some variables of the class.
> Since this is not permitted I thought I might solve the problem by
> declaring it lazy. But this is still rejected by the compiler.
> If there are no circular dependencies in initialization of lazy variables
> this could be allowed, couldn’t it?
> Or is this a very big if?
>
> My workaround is ugly: a calculated property + a private variable + an
> initialized:Bool variable.
>
> Jan E.
>
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160704/ba315580/attachment.html>


More information about the swift-users mailing list