iOS Swift 3:这种情况下会发生retain cycle吗?

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

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"

其次,警报操作块也应该对自身有弱引用

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
    }