0

Currently I'm following the nice book App Development with Swift and so far everything is explained awesome. Nevertheless following the exercises I found certain piece of code that I'd love to understand much better. I've already tried to look information everywhere but I don't even know what's the name or how to find it. So with no more to say, it's the following:

I have a dictionary:

var frequencyOfAnswers: [AnimalType: Int] = [:]

AnymalType is simply an enum with four cases. Then they perform a mapping on frequencyOfAnswers:

let responseTypes = responses.map { $0.type }

An so far everything is good. But in the following iteration:

for response in responseTypes {
    frequencyOfAnswers[response] = (frequencyOfAnswers[response]
    ?? 0) + 1
}

I cannot comprehend totally the use of ??. I know that here the point is that if the new element is not already into the dictionary, then is gonna be added and it'll have a value of 1. And if it already exists, then its correspondent integer values is gonna increase by 1.

I'd like to have little more information about this syntax. If anyone of you guys could provide me with a good text, web page or explanation I'd be really thankful.

Thank you in advance.

Waldi
  • 39,242
  • 6
  • 30
  • 78

2 Answers2

2

?? is called a "nil-coalescing" operator.

Quite simply, it checks an optional variable to see if it has a value, and:

  • if it does, the value is unwrapped and used
  • if it does not, the value to the right of the operator is used instead.

For example:

var cheese: String?
var tomatoes = cheese ?? "bread"

In this case, the variable tomatoes equals "bread" because the variable cheese was never assigned a variable.

If you did:

var cheese: String?
cheese = "lettuce"
var tomatoes = cheese ?? "bread"

Now tomatoes equals "lettuce" because we gave cheese a value. And because cheese is an optional, the value is automatically unwrapped (meaning we can use its value directly).

If you want to read more about it, check out Apple's documentation.

brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • Sorry, wasn’t intending to delete any of your content, just appending the link. I must have fat-fingered it. – Alan Kantz Nov 17 '17 at 19:56
1

?? is the Nil-Coalescing Operator.

The nil-coalescing operator (a ?? b) unwraps an optional a if it contains a value, or returns a default value b if a is nil. The expression a is always of an optional type. The expression b must match the type that is stored inside a.

The nil-coalescing operator is shorthand for the code below:

a != nil ? a! : b

It is used to define a default value for an Optional type that has no value. It is being used in your example to set the frequencyOfAnswers dictionary value to 0 for cases where the frequencyOfAnswers dictionary has no value for a given response type key.

Read more in the Nil-Coalescing Operator section of https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html

Michael Peterson
  • 10,383
  • 3
  • 54
  • 51