0

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()

    }
}
Santiago Alvarez
  • 167
  • 1
  • 12

1 Answers1

0

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.

Wyetro
  • 8,439
  • 9
  • 46
  • 64