Reputation: 10038
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)
}
Upvotes: 0
Views: 479
Reputation: 1482
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.
Upvotes: 0
Reputation: 2237
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"
Upvotes: 0
Reputation: 4941
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
.
Upvotes: 4
Reputation: 10662
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?
Upvotes: 2
Reputation: 192
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.
Upvotes: 2