[ios] スウィフトperformSelector:withObject:afterDelay:


0 Answers

あなたはこれを行うことができます:

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

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

SWIFT 3

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

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

この質問には既に回答があります:

私は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' is unavailable: 'performSelector' methods are unavailable

どのメソッドを呼び出すために呼び出すのですか?

更新

私が結局何をしたのですか?

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



Related