I have read Optional Chaining chapter of apple's The Swift Programming Language(swift2). In this chapter, there is no mention about an optional question mark after a function name but before the left parenthesis.
But I saw the following swift code from this Apple's document (the 'Delegation' section):
//There is a question mark right after 'window'
if let fullScreenSize = myDelegate?.window?(myWindow, willUseFullScreenContentSize: mySize) {
print(NSStringFromSize(fullScreenSize))
}
What does it mean of having a question mark after a function name but before the left parenthesis ?
Ternary Operator operator is also called the ternary operator because, unlike other operators such as strict equal ( === ) or remainder ( % ), it's the only one that takes three operands. Starting with ? , we add a condition on the left side and a value on the right side to return when the condition is true.
Optional chaining (?.) The optional chaining operator ( ?. ) accesses an object's property or calls a function. If the object is undefined or null , it returns undefined instead of throwing an error.
Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil . If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil , the property, method, or subscript call returns nil .
It's also known as the ternary conditional operator because it combines some of what the ternary operator does together with the chaining operator, normally called dot notation. It's similar to the traditional dot syntax which lets you access an object and its properties.
There are two situations in which this it used:
A protocol method is itself marked optional
, so we don't know whether the adopter of the protocol implements this method.
We are sending a message to an AnyObject. We can send any known class message to an AnyObject — it throws away type-checking — but then, again, we don't know whether the actual object implements this method.
So this question mark means to send this message optionally and safely. If it turns out that the recipient does not implement it, don't send the message, and return nil. If the recipient does implement it, send the message, but now we have to wrap any result in an Optional (because otherwise we could not return nil
in the first case).
Behind the scenes, Objective-C respondsToSelector:
is being called. Hence, this pattern is available only if the recipient is exposed to Objective-C. Basically, this is an Objective-C language feature expressed in Swift shorthand.
There is also the case of so-called "failable initializers". That's when you want to provide a type for which initialization can fail under some conditions like invalid initialization parameter values or a required external source. “A failable initializer creates an optional value of the type it initializes" (The Swift Programming Language (Swift 5.5)). One typical example is the String
type initializer init?(data: Data, encoding: String.Encoding)
that initializes a String
value from a Data
byte buffer. If the provided Data
can't be decoded using the selected encoding value (e.g. UTF-8) the initialization will fail and it will return nil
.
See https://docs.swift.org/swift-book/LanguageGuide/Initialization.html#ID224
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With