범위에 속성이 아닌 변수가 있고 내부 범위에서 재정의하면 내부 범위에서 원래 변수에 액세스하는 방법이 있습니까? 예를 들면 다음과 같습니다.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("awesomeCell", forIndexPath: indexPath) as! AwesomeTableViewCell
cell.delegate = self
<outscope>.cell = cell
} else {
cell = UITableViewCell()
}
return cell
}
<outscope>
여기 self
속성 과 같은 것 입니다. 이것을 할 방법이 있습니까?
코드 let cell = ...
에서 if 블록 에 외부 범위에서 변수 cell
를 "숨기기"또는 "그림자" 하는 새 변수 가 도입 cell
됩니다. 내가 아는 한 같은 이름을 가진 외부 변수에 액세스하는 언어 기능이 없습니다.
즉시 평가 된 클로저를 사용하여 비슷한 효과를 얻을 수 있습니다.이 클로저는 로컬 범위에서 셀을 만들고 구성하고 결과를 다시 외부 범위로 전달합니다.
let cell: UITableViewCell
if indexPath.section == 0 {
cell = {
let cell = tableView.dequeueReusableCellWithIdentifier("awesomeCell", forIndexPath: indexPath) as! AwesomeTableViewCell
cell.delegate = self
return cell
}()
} else {
// ...
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다