gpt4 book ai didi

iOS swift 3 : does retain cycle happens in this case?

转载 作者:行者123 更新时间:2023-11-30 12:22:51 24 4
gpt4 key购买 nike

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CheckoutCell") as! CheckoutCell

let product = shoppingCart[indexPath.row]

var tfQuantity : UITextField!
cell.clickEditAction = { [weak self] celll in
guard let ss = self else { return }
let alert = UIAlertController(title: nil, message: "Enter new quantity", preferredStyle: .alert)

alert.addTextField { (textfield) in
tfQuantity = textfield
}

let okAction = UIAlertAction(title: "OK", style: .default) { (action) in
if tfQuantity.text == ""{
return
}

if let newQuantity = Int(tfQuantity.text){
product.quantity = newQuantity
self.tbvCheckout.reloadData()
}
return
}

alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}

return cell
}

这行代码:

self.tbvCheckout.reloadData()

如果我不使用 [weak self] 或 [unowned self],它是否会在当前对象和 UIAlertAction 实例之间创建保留周期?如果我改用以下代码会怎样:tableView.reloadData()?

最佳答案

有几件事:

首先,您创建了一个弱引用,但我没有看到您在代码中使用它。

guard let ss = self else { return }

对 self 的任何引用都应该通过您创建的弱 self 变量 "ss" 进行。

其次,警报操作 block 也应该具有对 self 的弱引用

let okAction = UIAlertAction(title: "OK", style: .default) { [weak self] (action) in
if tfQuantity.text == ""{
return
}

if let newQuantity = Int(tfQuantity.text){
product.quantity = newQuantity
self?.tbvCheckout.reloadData()
}
return
}

关于iOS swift 3 : does retain cycle happens in this case?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44583564/

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