Hello Apple Developer Community,
First of all, I am a total newbie to the Swift programming language.
I am trying to ship my first app with Xcode. I thought it would make sense to first educate myself about the basics of Xcode and Swift. I've been reading this article as a starting point: https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/index.html#//apple_ref/doc/uid/TP40015214-CH2-SW1
Coming from java, I find the syntax of Swift to be extremely complicated, obscure, unintuitive, and frankly hard to understand.
Check the following statement for example, found in Working with Table Views >> Implement Navigation
guard let button = sender as? UIBarButtonItem, button === saveButton else {
os_log("The save button was not pressed, cancelling", log: OSLog.default, type: .debug)
return
}
So I have a few questions about the above code:
I understand that the as keyword is the down-cast operator. But why is there a "?" after it? What's the difference between as and as?
I thought the else clause comes immediately after the initial statement, in this case button = sender. What's the equality check button === saveButton after the comma? Does this mean that the downcast has to work AND button must equal saveButton in order for the else clause at the end to not fire?
Many thanks in advance! And thank you for your patience 😉
KC