Swift 3.0 unresolved identifier for DispatchQueue

你说的曾经没有我的故事 提交于 2019-12-02 04:38:33

问题


I have been trying to find the answer to this for hours now but still no avail. I am trying to use the following code:

func fetchPosts() {
    ref.child("Amore").child("Posts").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
            let postMod = ReviewsPostModel()

            postMod.setValuesForKeysWithDictionary(dictionary)
            self.posts.insert(postMod, atIndex: 0)
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
            self.tableView.reloadData()
        }
    })
}

In Swift 3.0.1, but it is saying that "DispatchQueue" is an unresolved identifier. I have imported Dispatch, and made sure that the class was pointed at the right target file. Any and all help is greatly appreciated.


回答1:


Although you say you are using Swift 3.0.1, you are not. You are using Swift 2.3. Thus, there is no such thing as DispatchQueue. Your code needs to be written like this:

let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.2 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
    self.tableView.reloadData()
}



回答2:


Recently, a simple swift script I had written was yielding this error when I ran it under Linux. The same script had worked fine under macOS.

Out of curiosity, I added an 'import Dispatch' to the top of the script and it then ran fine on Linux.



来源:https://stackoverflow.com/questions/40825470/swift-3-0-unresolved-identifier-for-dispatchqueue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!