gpt4 book ai didi

Swift 替代 performSelectorOnMainThread

转载 作者:IT王子 更新时间:2023-10-29 05:15:08 32 4
gpt4 key购买 nike

我想用这个方法在一个 block 中重新加载我的表数据:

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 block 不会执行。我得到的错误是:

'performSelectorOnMainThread' is unavailable: 'performSelector' methods are unavailable

那么在 swift 中 'performSelectorOnMainThread' 的替代方案是什么?

更新:

我现在遇到中止错误。

最佳答案

这个简单的 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()

})

在 viewDidLoad() 中?

关于Swift 替代 performSelectorOnMainThread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24126261/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com