gpt4 book ai didi

ios - 这是变量还是函数?或两者?

转载 作者:行者123 更新时间:2023-11-28 11:00:40 25 4
gpt4 key购买 nike

我正在关注 this教程和入门项目包含一个 UITableViewCell 类,代码如下:

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()
}
}
}

这看起来像一个没有参数的函数。我以前从未见过带有外壳的变量。第一行是一个 var 语句,但开始一个附件:

var product: SKProduct? {

谁能解释一下?

最佳答案

在这个例子中,product是一个变量,didSet是一个函数。更具体地说,didset 是一个 property observer .来自 Apple 的文档:

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.

因此,只要您进行变量赋值,didSet 中的代码就会执行,例如:

product = someSKProduct

关于ios - 这是变量还是函数?或两者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40753858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com