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]
의 순환 참조 방지当前VC(self) -> UIAlertController -> 闭包 -> 当前VC
또한 클로저 내의 ac 매개변수도 약하거나 소유되지 않아야 합니다
Apple의 공식 문서를 확인하실 수 있습니다
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
이 코드의 경우
unowned
은 필요하지 않습니다. 클로저는self
에 대한 참조를 보유하지만self
은 클로저에 대한 참조를 보유하지 않으므로 순환 참조를 구성하지 않습니다.