func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let person = people[indexPath.item]
let ac = UIAlertController(title: "Rename person", message: nil, preferredStyle: .Alert)
ac.addTextFieldWithConfigurationHandler(nil)
ac.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
ac.addAction(UIAlertAction(title: "OK", style: .Default) { [unowned self, ac] _ in
let newName = ac.textFields![0]
person.name = newName.text!
self.collectionView.reloadData()
})
presentViewController(ac, animated: true, completion: nil)
}
在上面這段代碼中為什么需要[unowned self]
歡迎選擇我的課程,讓我們一起見證您的進(jìn)步~~
Prevent 當(dāng)前VC(self) -> UIAlertController -> 閉包 -> 當(dāng)前VC
circular references
In addition, the ac parameter within the closure should also be weak or unowned
developer.apple.com/library
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-ID48
For this code, there is no need. unowned
,雖然閉包持有self
的引用,但是self
does not hold a reference to the closure, so it does not constitute a circular reference.