Why is our URLSession object a variable and not a constant

151 views Asked by At

I'm following a tutorial on consuming REST API's to get JSON back from a third party server. We are building out a networkProcessor class in which we will be able to use it by creating instances of this class in ViewControllers in order to make url sessions for network calls. I noticed that the author declared the session placeholder as a variable and not a constant which went against what I've learned while using Xcode which is to always consider using constants first. Is there a specific reason why the author may have chosen to make this a variable and not a constant?

Here is what I have so far:

import Foundation


class NetworkProcessor {


    lazy var configuration: URLSessionConfiguration = URLSessionConfiguration.default
    lazy var session: URLSession = URLSession.init(configuration: self.configuration)

    let url: URL

    //since it is a class we need to initalize this class
    init(url: URL) {
        self.url = url
    }
}
1

There are 1 answers

0
wm.p1us On

When you use keyword lazy you can't use it with let keyword at the same time. Lazy properties must be variables but not constants.