Vyacheslav Vyacheslav - 2 months ago 22
Swift Question

ObjC [NSMachPort port] into Swift translation

I can't translate this Objective-C code into Swift

NSRunLoop *runloop = [NSRunLoop currentRunLoop];
[runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];


The problem is about
[NSMachPort port]
. How it looks in Swift?

This code as shown as incorrect:

let runloop:NSRunLoop = NSRunLoop.currentRunLoop()
runloop.addPort(NSMachPort.port, forMode: NSDefaultRunLoopMode)


Neither
NSMachPort.port
or
NSMachPort.port()
are correct.

Does
NSPort()
the same as
[NSMachPort port]
?

Answer

NSMachPort is a subclass of NSPort rather than the latter being an improved name, so NSPort() will not construct an NSMachPort.

NSMachPort() is the equivalent of [NSMachPort port].