Reputation: 12206
I'm following this tutorial and the starter project includes a UITableViewCell class with this code:
var product: SKProduct? {
didSet {
guard let product = product else { return }
textLabel?.text = product.localizedTitle
if RageProducts.store.isProductPurchased(product.productIdentifier) {
accessoryType = .checkmark
accessoryView = nil
detailTextLabel?.text = ""
} else {
ProductCell.priceFormatter.locale = product.priceLocale
detailTextLabel?.text = ProductCell.priceFormatter.string(from: product.price)
accessoryType = .none
accessoryView = newBuyButton()
}
}
}
This looks like a function without parameters. I've never seen a variable with an enclosure before. The first line is a var statement but starts an enclosure:
var product: SKProduct? {
Can anyone explain this?
Upvotes: 0
Views: 65
Reputation: 393
In this example, product
is a variable, and didSet
is a function. More specifically, didset
is a property observer. From Apple's documentation:
Property observers observe and respond to changes in a property’s value. Property observers are called every time a property’s value is set, even if the new value is the same as the property’s current value.
So the code inside of didSet
will get executed any time you make a variable assignment, like:
product = someSKProduct
Upvotes: 3