100

In Swift programming I found some question marks with objects.

var window: UIWindow?

Can anybody explain the use of it?

shim
  • 9,289
  • 12
  • 69
  • 108
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
  • 38
    @Vladimir,angelo can't i learn from stackOverflow? – Rajneesh071 Jun 05 '14 at 10:10
  • 1
    I did not downvote you, but I do think the downvotes are due. You are asking such a basic question that it's hard to believe you have done due diligence and tried to find the answer from a language reference (the fact that there is currently just one language reference means that you cannot even say your reference book did not explain it well: it's in fact one of the first things that it explains). – Analog File Jun 05 '14 at 10:14
  • 1
    Downvotes doesn't necessarily mean a question shouldn't be here. They are a statement on their quality and the research effort put into them. You can learn through Stack Overflow instead of going through the language reference. It's your choice. But the downvotes might be the price. – Cezar Jun 05 '14 at 10:16
  • 4
    But this question will helpful for other user also, because no one have such time to read book.. – Rajneesh071 Jun 05 '14 at 10:26
  • 12
    @MattGibson - Now a day everyone just type query on Goole, for this basic thing who will search the complete book? like if i want to know that how to addObject in array, then i will just google it, no one will search pages of complete book to get a basic question, i also seared this question before on google, but didn't found any reliable solution. – Rajneesh071 Jun 05 '14 at 13:04
  • 2
    @Rajneesh071 it is because it's so basic and available people didn't need the help of others to answer that question – Angelo Jun 05 '14 at 13:06
  • @Angelo - Ok , but my message for the down-voter that go to this question if you really want to down-vote basic question http://stackoverflow.com/questions/899209/how-do-i-test-if-a-string-is-empty-in-objective-c – Rajneesh071 Jun 05 '14 at 13:08
  • Seriously? Testing if a string is empty in Objective C sometimes results to false positive even with right syntax or logic. Basic yes, definitive no. Your question however is answered definitively in the iBook guide. – Angelo Jun 10 '14 at 03:26
  • 1
    Basically means the kind of variable which either has a value OR is nil, representing it does not have a value. Refer to The Swift Programming Language, available on iBooks – Bijay Koirala Jul 31 '14 at 11:39

3 Answers3

68

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.

Salman Zaidi
  • 9,342
  • 12
  • 44
  • 61
  • I think the result of `greeting` is "Hello, John Appleseed" while if `optionalName` is `nil` the result will be "Hello!", the comma and exclamation mark actually help me to understand it better – theo123490 Apr 07 '20 at 00:47
23

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.

Cezar
  • 55,636
  • 19
  • 86
  • 87
  • let posString = "123" let convertString = posString.Int() convertString Value of type 'String' has no number 'Int' – Solid Soft May 25 '16 at 07:04
2

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 .

Kumar KL
  • 15,315
  • 9
  • 38
  • 60