idea
Language
init/deinit> Constructor/destructor===> identity operator, validates that both references are to the same object.
Classes and structs
Structs are value based (enums are also value based). Assignment is made by making a copy. let on a struct prevents mutating it. Classes are reference based, assignment is made by copying the pointer.
Use structs by default. Unless:
- Need Obj-C interop
- Need to control identity (
===)
Protocols can be implemented by both classes and structs.
Exceptions
Methods that throw are identified by the throws keyword (eg func load async throws)
Try is used to call a method that throws. try? returns nil if an exception is triggered, which allows to use guard
try/catch is do {} catch {} where the error is in a special variable called error
References
Swift uses Automatic Reference Count (ARC)[1][^2]. Strong references are counted, ++ when new reference, – when reference removed. When the reference count goes to 0, the object is deallocated.
weak keyword allows to create a weak reference to a variable, which doesn't prevent it from being de-initialized. unowned is similar, but doesn't make the value as nil if it's deallocated. This should only be used when we're sure the value isn't gonna be deallocated. weak is equivalent to an optional unowned.
links
references
[1]: https://www.advancedswift.com/strong-weak-unowned-in-swift/
[2]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/automaticreferencecounting/
[3]: https://developer.apple.com/documentation/swift/choosing-between-structures-and-classes