Are these two snippets equivalent?

30 Views Asked by At

Are both of this snippets the same? Is it possible that on the first one myClass can at some point in the lifetime of the application will be eliminated?

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    private let myClass = MyClass()

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        myClass.doSomething()

    }
}
    

and

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    private var myClass: MyClass?

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        myClass = MyClass()
        myClass?.doSomething()

    }
}
1

There are 1 best solutions below

2
Wyetro On BEST ANSWER

No the two snippets are not equivalent. You can not reassign a let in Swift. In your second snippet the line:

private let myClass: MyClass?

should be:

private var myClass: MyClass?

Assuming that your two snippets don't include any other code that could interact with the variable then it would be the same.