ios Swift performSelector:withObject:afterDelay: 不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24170282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:49:53  来源:igfitidea点击:

Swift performSelector:withObject:afterDelay: is unavailable

iosobjective-cswiftxcode6

提问by Cody Winton

I have an app in Objective C that I'm transitioning to Swift. In Objective C, I have this method:

我在 Objective C 中有一个应用程序,我正在过渡到 Swift。在目标 C 中,我有这个方法:

[self.view performSelector:@selector(someSelector) withObject:self afterDelay:0.1f];

[self.view performSelector:@selector(someSelector) withObject:self afterDelay:0.1f];

I'm working with Swift and I can't figure out how to do this. I've tried:

我正在与 Swift 合作,但我不知道如何做到这一点。我试过了:

self.view.performSelector(Selector("someSelector"), withObject: self, afterDelay: 0.1)

self.view.performSelector(Selector("someSelector"), withObject: self, afterDelay: 0.1)

Here's the error that I get: 'performSelector' is unavailable: 'performSelector' methods are unavailable

这是我得到的错误: 'performSelector' is unavailable: 'performSelector' methods are unavailable

What call would I use to call a method afterDelay?

我将使用什么调用来调用方法afterDelay

UPDATE

更新

Here's what I ended up with:

这是我的结果:

extension NSObject {

    func callSelectorAsync(selector: Selector, object: AnyObject?, delay: NSTimeInterval) -> NSTimer {

        let timer = NSTimer.scheduledTimerWithTimeInterval(delay, target: self, selector: selector, userInfo: object, repeats: false)
        return timer
    }

    func callSelector(selector: Selector, object: AnyObject?, delay: NSTimeInterval) {

        let delay = delay * Double(NSEC_PER_SEC)
        let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
        dispatch_after(time, dispatch_get_main_queue(), {
            NSThread.detachNewThreadSelector(selector, toTarget:self, withObject: object)
        })
    }
}

回答by brandonscript

Swift 4

斯威夫特 4

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
    // your function here
}

Swift 3

斯威夫特 3

DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(0.1)) {
    // your function here
}

Swift 2

斯威夫特 2

let dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC))) 
dispatch_after(dispatchTime, dispatch_get_main_queue(), { 
    // your function here 
})

回答by StevenOjo

You could do this:

你可以这样做:

var timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("someSelector"), userInfo: nil, repeats: false)

func someSelector() {
    // Something after a delay
}

SWIFT 3

斯威夫特 3

let timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(someSelector), userInfo: nil, repeats: false)

func someSelector() {
    // Something after a delay
}

回答by Tommy

Swift is statically typed so the performSelector:methods are to fall by the wayside.

Swift 是静态类型的,因此performSelector:方法将被搁置一旁。

Instead, use GCD to dispatch a suitable block to the relevant queue — in this case it'll presumably be the main queue since it looks like you're doing UIKit work.

相反,使用 GCD 将合适的块分派到相关队列——在这种情况下,它可能是主队列,因为它看起来像是在做 UIKit 工作。

EDIT: the relevant performSelector:is also notably missing from the Swift version of the NSRunLoopdocumentation("1 Objective-C symbol hidden") so you can't jump straight in with that. With that and its absence from the Swiftified NSObjectI'd argue it's pretty clear what Apple is thinking here.

编辑:文档的 Swift 版本中performSelector:也明显缺少相关内容(“隐藏 1 个 Objective-C 符号”),因此您无法直接使用它。有了它以及它在 Swiftified 中的缺席,我认为很清楚 Apple 在这里的想法。NSRunLoopNSObject