Swiftでの遅延実行について、まとめた。

遅延実行

下記の遅延実行の方法1または、遅延実行の方法2により、遅延実行ができます。

遅延実行の方法1

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(), {
    //処理
} )

Swift 3

let dispatchTime = DispatchTime.now() + 0.1

DispatchQueue.main.asyncAfter( deadline: dispatchTime ) {
    //処理
}

遅延実行の方法2

Swift 2

NSTimer.scheduledTimerWithTimeInterval( 0.1, target: self, selector:"delaySelector", userInfo: nil, repeats: false );

func delaySelector() {
    //処理
}

Swift 3

Timer.scheduledTimer( timeInterval: 0.1, target: self, selector: #selector(delaySelector), userInfo: nil, repeats: false )

func delaySelector() {
    //処理
}

参考

http://stackoverflow.com/questions/24170282/swift-performselector-withobject-afterdelay

Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.