What is the meaning of the question mark "?" in Swift?


In this tutorial, you will learn what is a question mark in the Swift language, how to use it, and what it is used for.

What does the question mark indicate?

This question mark indicates that a variable might contain a nil value or some value of type declared with the variable.

Before learning what a question mark is, let's understand what are optionals in Swift.

Optionals

When you create a variable in the Swift language, either it can contain an initial value or it might be holding nothing at the initial point. In that case, you use optionals that represent a variable with two possible situations: either a value or no value at all.

Note − In Objective-C, there is no concept of optional.

Let's understand it with an example −

var possibleNumber = "450"
var convertedNumber = Int(possibleNumber)

In the above example, there is an input string that we are converting into an Int. However, this string might or might not be a valid integer number. In that case, once we convert this string into an integer using the Int() function, it will return an optional integer value. This means the output can be nil or a valid integer value.

How to define an optional variable?

var possibleMessage: String?

It's imperative to understand the meaning of this line. Here is a variable possibleMessage of type string. In the question mark after the string word, there is an indication that the variable might contain a valid string value or might have no value.

Why do we need to use a question mark?

var errorMessage: String = "Server not found"

In the above code line, we define a variable of string type with a value. Now, you want to set this variable to nil as you don't need this value for any reason. Let's assign a nil value −

errorMessage = nil

Build the code with this above line, and you will see there is a compile error coming like the below −

error: 'nil' cannot be assigned to type 'String'

The error shows clearly that you cannot assign a nil value to a string-type variable.

Note − You cannot assign nil to non-optional variables or constants.

Then how can I assign a nil value to this variable? Yes, you can assign a nil value by making this variable optional like below −

var errorMessage: String? = "Server not found"

Now you can assign a nil value because you have told the compiler in advance that this variable might contain a nil value.

errorMessage = nil
print(errorMessage)

Output

nil

Unwrap Value

Value can be obtained in many ways, but each method is used in a different situation.

Using Forced Unwrapping (not recommended)

In this method, you can access the value by adding an exclamation point (!) after the variable name. Like below −

Example

var errorMessage: String? = "Invalid URL found" print(errorMessage!)

Output

Invalid URL found

If you know an optional variable contains a value, then you can use it (!), otherwise, the app will crash if the variable does not contain a value. Simply an exclamation point says, "I know that this optional definitely has a value; please use it."

There is a way to check first and use an exclamation point with optional variables like below −

Example

var errorMessage: String? = "Invalid URL found" if errorMessage != nil { print("error message is not nil: \(errorMessage!)") }

Output

error message is not nil: Invalid URL found

Explanation

First, we declare an optional variable of type string. Next, we check that the variable is not equal to nil. If not nil, print the value of the variable. In this way, your app will not crash because you have already checked for it.

Optional Bindings (recommended)

To read the value of an optional variable, this is the recommended method. In this method, you check whether the variable has a value or not. If a value is found, you hold the value in a local constant or variable. Let's see an example −

Example

var errorMessage: String? = "Invalid URL found" if let message = errorMessage { print("Error message: \(message)") }

Output

Error message: Invalid URL found

Here, you are storing the value in a temporary constant 'message' and then using it. If errorMessage has no value or is nil, then the if condition will not be true. So, you can see this is a very safe method to unwrap a value from optional variables.

Conclusion

Swift’s nil is not the same as Objective-C nil. In Objective-C, nil is a pointer which is pointing to an object which does not exist in memory. In Swift, nil is not a pointer and it points to a value of a certain type that does not exist in memory.

Updated on: 22-Nov-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements