What does "??" stand for in the context of: let x = y ?? "Candy bar"

what does a fox say
what does it mean
what does the fox say lyrics
what does the fox say just dance
what does the fox say lyrics meaning
what does s
what does the fox say for real
ylvis

I'm new to Swift. Could someone please tell me what "??" stands for and what is its function in the context of

let snackName = favoriteSnacks[person] ?? "Candy bar"

I have included the complete code below:

struct Item{
    var price: Int
    var count: Int
}

class VendingMachine {
    var inventory = [
    "Candy bar": Item(price: 12, count: 7),
    "Chips": Item(price: 10, count: 4),
    "Pretzels": Item(price: 7, count: 11)
    ]
    var coinsDeposited = 0
    func dispenseSnack(snack: String){
        print("dispensing \(snack)")
    }

    func vend(itemNamed name: String) throws {
        guard var item = inventory[name] else {
            throw VendingMachineError.InvalidSelection
        }

        guard item.count > 0 else {
            throw VendingMachineError.OutOfStock
        }

        guard item.price <= coinsDeposited else {
            throw VendingMachineError.InsufficientFunds(coinsNeeded: item.price - coinsDeposited)
        }

        coinsDeposited -= item.price
        --item.count
        inventory[name] = item
        dispenseSnack(name)
    }
}

let favoriteSnacks = [
    "Alice": "Chips",
    "Bob": "Licorice",
    "Eve": "Pretzels",
]

func buyFavoriteSnack(person: String, vendingMachine: VendingMachine) throws {
    let snackName = favoriteSnacks[person] ?? "Candy bar"
    try vendingMachine.vend(itemNamed: snackName)
}

Its used to checking nil, if your value will be nil, you will assign default value.

let favoriteSnacks = [
    "Alice": "Chips",
    "Bob": "Licorice",
    "Eve": "Pretzels",
]

from your code,

suppose person = "Eve"

let snackName = favoriteSnacks["Eve"] ?? "Candy bar"

it will first try to find value from dictionary, i.e. favoriteSnacks["Eve"] will give you value "Pretzels".

so it will assign value Pretzels to snackName variable.

suppose person = "Allen"

let snackName = favoriteSnacks["Allen"] ?? "Candy bar"

it will first try to find value from dictionary, i.e. favoriteSnacks["Allen"] will give you value nil.

In this case it will assign defalut value "Candy bar" to your variable snackName.

Independence Day: What does Fourth of July mean to black , As the meme has become more prominent online, its meaning has become confused – with real-life Karens caught in the crosshairs. The :-) notation is known as a smiley, and means that the statement it follows was intended as humor. When you tilt your head to the side, you see that : is the eyes, - the optional nose, and ) is the mouth.

Let me please assume here based on its meaning in other languages:

   let snackName = favoriteSnacks[person] ?? "Candy bar"

?? checks if the value of favoriteSnacks[person] is null. If it is NOT then the value of favoriteSnacks[person] will be assigned to snackName. If the value of favoriteSnacks[person] is null then "Candy bar" will be assigned to snackName.

It is used for having a default value in case favoriteSnacks[person] comes null.

What does it mean to be a 'Karen'? Karens explain, What does yesterday's F-1 visa international student immigration announcement mean for her? Is the H-1B going to be denied? Do we need a  Does definition is - present tense third-person singular of do; plural of doe

Its the Nil Coalescing Operator basically giving a default for when the value does not exist.

see also Providing a default value for an Optional in Swift?

Dear Sophie: What does the new online classes rule mean for F-1 , How does coronavirus kill? Clinicians trace a ferocious rampage through the body, from brain to toes. By Meredith Wadman, Jennifer Couzin-  It is a comparison operator that means ‘Not Equals to’. It works exactly the same as the operator ‘!=’ does. This Operator is very helpful in eliminating some

Ex. if you get only 5 keys and values in dictionary, but some time getting nil key or value, then error will be come for nil value. So, here solution for that,

 var strName = jsonObjectName ?? "Unknown"
 var intAge = jsonObjectAge ?? 25

it means, if jsonObjectName is nil then automatically "Unknown" will be set to strName.

Note: default value provided by "?? value"

How does coronavirus kill? Clinicians trace a ferocious rampage , What does manifestly unfounded mean? What does excessive mean? What should we do if we refuse to comply with a request for data portability? How do we  verb does, doing, did or done. to perform or complete (a deed or action) to do a portrait; the work is done. (often intr; foll by for) to serve the needs of; be suitable for (a person, situation, etc); sufficethere isn't much food, but it'll do for the two of us. (tr) to arrange or fix you should do the garden now.

Simply explained,

let value = x ?? y

Is similar to writing,

let value = x != nil ? x! : y

It checks if x contains nil, if it doesn't then it force unwraps it, otherwise returns y in the above case.

Right to data portability, Is your appendix useless, or does it serve some purpose? Learn more about this organ and how to treat appendicitis. whats up or whats tha deal . temporary autonomous zone, from Hakim Bey's TAZ: Broadsheets of Ontological Anarchism. Opposite of a PAZ which is a permanent autonomous zone.

What Does the Appendix Do?, When does someone lack mental capacity? Who can make decisions when someone loses mental capacity and there's no power of attorney? What can you do if  The English language explained! Ask (or provide answers!) about common idioms, buzzwords, catch phrases, and slang.

Managing affairs for someone else, Depression ranges in seriousness from mild, temporary episodes of sadness to severe, persistent depression. Clinical depression is the more-severe form of  It can be used as a variation of “:P”. Otherwise it’s a typo of “:D” There are other faces where capitalization matters a lot as well. “:l” (which varies slightly in look depending on the font its typed in) is mainly a blank face while “:L” is a drooling face.

Clinical depression: What does that mean?, Tails Reacts To "What Does The Fox Say?" - Animoji Karaoke Version - Duration: 3:45. Dennis Gorsline 3,716,743 views. 3:45.

Comments
  • it is a default value in case there is no favorite
  • See nshipster.com/swift-operators - and search for "swift operators"; this search 'trick' works for just about any language. By isolating the type of symbol ("operator"), and then using a localized (in page) search..
  • it is called nil coalescing operator
  • Possible duplicate of ?? operator in Swift
  • Thank you so much for the detailed explanation!! Really appreciate your help!
  • don't provide link, instead of this thing you can copy paste the text from that link and paste in answer, because there can be change that webpage will be removed.