Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional chaining question mark after function name

Tags:

ios

swift

swift2

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 ?

like image 956
user842225 Avatar asked Sep 19 '15 16:09

user842225


People also ask

What does ?: Mean in JavaScript?

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.

What does question mark after variable mean in JavaScript?

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.

What is the optional chaining?

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 .

Is optional chaining is same as ternary operator?

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.


2 Answers

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.

like image 117
matt Avatar answered Nov 05 '22 12:11

matt


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

like image 20
fabio maia Avatar answered Nov 05 '22 12:11

fabio maia