我有以下代碼:斯威夫特價值封閉內消失
let context = DBHelper.privateContext()
context.performBlock({ [weak self] in
if let ss = self {
//1.
let articles = DBHelper.clientFetchArticles(context)
//2.
dispatch_async(dispatch_get_main_queue(), {
//3.
ss.articles = articles
ss.tableView.reloadData()
ss.refreshControl.endRefreshing()
})
}
})
調用tableView.reloadData()
後,下面的代碼運行:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ArticleCell", forIndexPath: indexPath) as! ArticleCell
#4
let article = articles[indexPath.row]
cell.setupWithArticle(article)
return cell
}
DBHelper.clientFetchArticles
將返回[Article]
有2篇文章(類型ManagedObject的) 。
在#1,當我打印
po articles[0].title
,我得到正確的title
打印出來。在#3,
po articles.count
打印2
,po articles[0].title
也是正確的在#4,
po self.articles[0].title
得到爲零。
我試過dispatch_sync(dispatch_get_main_queue())
但仍然是相同的結果。
你在#4試過'po self.articles [0]'嗎? – anhtu
是的,我已經試過了,它是一樣的('文章'是視圖控制器的屬性) – OMGPOP
它也是零? – anhtu