Swift替代performSelectorOnMainThread [英] Swift alternative to performSelectorOnMainThread
本文介绍了Swift替代performSelectorOnMainThread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在此方法中的块内重新加载我的表数据:
I want to reload my table data inside a block in this method:
import UIKit
import AssetsLibrary
class AlbumsTableViewController: UITableViewController {
var albums:ALAssetsGroup[] = []
func loadAlbums(){
let library = IAAssetsLibraryDefaultInstance
library.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupAll),
usingBlock: {(group, stop) in
if group {
self.albums.append(group)
}
else {
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
}, failureBlock: { (error:NSError!) in println("Problem loading albums: \(error)") })
}
override func viewDidLoad() {
super.viewDidLoad()
loadAlbums()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
//self.navigationItem.rightBarButtonItem = self.editButtonItem
}
但是else块不会执行。我得到的错误是:
But the else block will not execute. The error I get is:
'performSelectorOnMainThread' is unavailable: 'performSelector' methods are unavailable
那么在swift中替代'performSelectorOnMainThread'
是什么?
So what is the alternative to 'performSelectorOnMainThread'
in swift?
更新:
我现在收到中止错误。以下是控制台的外观: http://cl.ly/image/3D0h1x433P0R
I am now getting an abort error. Here is what the console looks like: http://cl.ly/image/3D0h1x433P0R
推荐答案
这个简单的C函数:
dispatch_async(dispatch_get_main_queue(), {
// DO SOMETHING ON THE MAINTHREAD
self.tableView.reloadData()
})
如何启动你的功能:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
loadAlbums()
})
$ b viewDidLoad()中的$ b
?
in viewDidLoad()?
这篇关于Swift替代performSelectorOnMainThread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文