gpt4 book ai didi

ios - 快速执行选择器 :withObject:afterDelay: is unavailable

转载 作者:IT老高 更新时间:2023-10-28 12:18:59 25 4
gpt4 key购买 nike

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

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

我正在使用 Swift,但我不知道该怎么做。我试过了:

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

这是我得到的错误:'performSelector' 不可用:'performSelector' 方法不可用

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

更新

这就是我最终得到的结果:

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)
})
}
}

最佳答案

swift 4

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

swift 3

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

swift 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
})

关于ios - 快速执行选择器 :withObject:afterDelay: is unavailable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24170282/

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