What the meaning of question mark '?' in swift? [duplicate]
You can use
if
andlet
together to work with values that might be missing. These values are represented asoptionals
. Anoptional
value either contains a value or containsnil
to indicate that the value is missing. Write a question mark (?) after the type of a value to mark the value asoptional
.If the optional value is
nil
, the conditional isfalse
and the code in braces is skipped. Otherwise, the optional value is unwrapped and assigned to the constant afterlet
, 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.
Question marks after a type refer to Optionals
, a way in Swift which lets you indicate the possibility that a value might be absent for any type at all, without the need for special constants.
It's used in the same situations you'd explicitly return nil
in Objective-C, when there is no object to be returned, or for values that are not objects, constants such as NSNotFound
. Optionals provide a consistent way of achieving this across all data types.
From the Apple provided iBook
You use optionals in situations where a value may be absent. An optional says:
- There is a value, and it equals x
or
- There isn’t a value at all
Here’s an example. Swift’s String type has a method called toInt, which tries to convert a String value into an Int value. However, not every string can be converted into an integer. The string "123" can be converted into the numeric value 123, but the string "hello, world" does not have an obvious numeric value to convert to.
let possibleNumber = "123"
let convertedNumber = possibleNumber.toInt()
// convertedNumber is inferred to be of type "Int?", or "optional Int"
Because the toInt method might fail, it returns an optional Int, rather than an Int. An optional Int is written as Int?, not Int. The question mark indicates that the value it contains is optional, meaning that it might contain some Int value, or it might contain no value at all. (It can’t contain anything else, such as a Bool value or a String value. It’s either an Int, or it’s nothing at all.)
There is a whole section on the language reference iBook on Optionals, and they are mentioned several times throughout the book. You should have a thorough look at it, since it's a fundamental concept of Swift programming, and one that is not prevalent in many other languages.
the optional Value of the type .
For Ex :
the optional version of the type casting operator : as?
,mean as
use for the type casting with might be optional to cast .