Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the meaning of question mark '?' in swift? [duplicate]

Tags:

ios

swift

In Swift programming I found some question marks with objects.

var window: UIWindow? 

Can anybody explain the use of it?

like image 349
Rajneesh071 Avatar asked Jun 05 '14 10:06

Rajneesh071


People also ask

What does double question mark mean in Swift?

Double question mark is a nil-coalescing operator. In plain terms, it is just a shorthand for saying != nil . First it checks if the the return value is nil, if it is indeed nil, then the left value is presented, and if it is nil then the right value is presented.

What does question mark do in Xcode?

A means added to source control, but not modified. M means it is added to source control and is modified. Also, if you see the question mark against a file that you know is tracked, and the Source Control->Refresh Status menu option doesn't change anything, try just quitting and restarting Xcode.

What does double question mark mean?

Answer and Explanation: The usage of multiple question marks is not punctually correct; however, double question marks are often used to emphasize the question that precedes it.

What is exclamation mark in Swift?

The bang or exclamation mark hints at potential danger. If you use an exclamation mark in Swift, you are about to perform an operation that can backfire. You are about to perform a dangerous operation and are doing so at your own risk. That is the meaning of the exclamation mark in Swift.


1 Answers

You can use if and let together to work with values that might be missing. These values are represented as optionals. An optional value either contains a value or contains nil to indicate that the value is missing. Write a question mark (?) after the type of a value to mark the value as optional.

If the optional value is nil, the conditional is false and the code in braces is skipped. Otherwise, the optional value is unwrapped and assigned to the constant after let, which makes the unwrapped value available inside the block of code.

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/pk/jEUH0.l

For Example:

var optionalString: String? = "Hello" optionalString == nil  var optionalName: String? = "John Appleseed" var greeting = "Hello!" if let name = optionalName {     greeting = "Hello, \(name)" } 

In this code, the output would be Hello! John Appleseed. And if we set the value of optionalName as nil. The if conditional result would be false and code inside that if would get skipped.

like image 80
Salman Zaidi Avatar answered Oct 09 '22 11:10

Salman Zaidi