This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let myOptional: Int? = 10 | |
if let myInt = myOptional { | |
print("myInt has a value of \(myInt).") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let myOptional: Int? = nil | |
let myInt = myOptional ?? 0 | |
print("myInt has a value of \(myInt).") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let myOptional: Int? = 10 | |
print("myOptional has a value of \(myOptional!).") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let myOptional: Int? = 10 | |
guard let myInt = myOptional else { | |
print("myInt has no value.") | |
} | |
print("myInt has a value of \(myInt).") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let myFirstOptional: Int? = 10 | |
let mySecondOptional: Int? = 20 | |
if let myFirstInt = myFirstOptional, let mySecondInt = mySecondOptional { | |
print("myFirstInt has a value of \(myFirstInt) and mySecondInt has a value of \(mySecondInt).") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Processor { | |
var modelNumber: Int | |
init() { | |
self.modelNumber = 21043 | |
} | |
} | |
struct Computer { | |
var processor: Processor? | |
init(processor: Processor?) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let center = NotificationCenter.default | |
center.addObserver(self, | |
selector: #selector(onNotificationReceived(_:)), | |
name: .notificationReceived, | |
object: nil) | |
@objc func onNotificationReceived(_ notification:Notification) { | |
if let data = notification.userInfo as? [String: Int] { | |
// Do something with the data | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let center = NotificationCenter.default | |
let notificationReceived = Notification.Name("notificationReceived") | |
let pizzas = ["Margherita": 1, "Pepperoni": 16, "Hawaii": 25] | |
center.post(name: notificationReceived, object: self, userInfo: pizzas) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func post(name aName: Notification.Name, | |
object anObject: Any?, | |
userInfo aUserInfo: [AnyHashable : Any]? = nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func addObserver(_ observer: Any, | |
selector aSelector: Selector, | |
name aName: Notification.Name?, | |
object anObject: Any?) |