The Run​Loop class declares the programmatic interface to objects that manage input sources. An Run​Loop object processes input for sources such as mouse and keyboard events from the window system, Port objects, and NSConnection objects. An Run​Loop object also processes Timer events.


Your application cannot either create or explicitly manage Run​Loop objects. Each Thread object—including the application’s main thread—has an Run​Loop object automatically created for it as needed. If you need to access the current thread’s run loop, you do so with the class method current.

Note that from the perspective of Run​Loop, Timer objects are not "input"—they are a special type, and one of the things that means is that they do not cause the run loop to return when they fire.


Accessing Run Loops and Modes

class var current:​ Run​Loop
var current​Mode:​ Run​Loop​Mode?

The receiver's current input mode.

func limit​Date(for​Mode:​ Run​Loop​Mode)

Performs one pass through the run loop in the specified mode and returns the date at which the next timer is scheduled to fire.

class var main:​ Run​Loop

Returns the run loop of the main thread.

func get​CFRun​Loop()

Returns the receiver's underlying CFRunLoop object.

Managing Timers

func add(Timer, for​Mode:​ Run​Loop​Mode)

Registers a given timer with a given input mode.

Managing Ports

func add(Port, for​Mode:​ Run​Loop​Mode)

Adds a port as an input source to the specified mode of the run loop.

func remove(Port, for​Mode:​ Run​Loop​Mode)

Removes a port from the specified input mode of the run loop.

Running a Loop

func run()

Puts the receiver into a permanent loop, during which time it processes data from all attached input sources.

func run(mode:​ Run​Loop​Mode, before:​ Date)

Runs the loop once, blocking for input in the specified mode until a given date.

func run(until:​ Date)

Runs the loop until the specified date, during which time it processes data from all attached input sources.

func accept​Input(for​Mode:​ Run​Loop​Mode, before:​ Date)

Runs the loop once or until the specified date, accepting input only for the specified mode.

Scheduling and Canceling Messages

func cancel​Perform(Selector, target:​ Any, argument:​ Any?)

Cancels the sending of a previously scheduled message.

func cancel​Perform​Selectors(with​Target:​ Any)

Cancels all outstanding ordered performs scheduled with a given target.


Run Loop Modes

NSRun​Loop defines the following run loop mode.


Inherits From