INADDR_LOOPBACK macro from & lt; netinet / in.h & gt; not imported into swift

advertisements

I'm trying to use the peertalk framework which has no documentation. On their obj-c example they use the INADDR_LOOPBACK macro, and example is working. But when i try to do the same in swift the system throw me an unresolved identifier error. Anyone knows how to fix it?

http://www.gnu.org/software/libc/manual/html_node/Host-Address-Data-Type.html


Update for Swift 3: As of Swift 3, INADDR_LOOPBACK is imported into Swift. Therefore it suffices to add

#include <netinet/in.h>

to the bridging header file, but a custom definition is not needed anymore.


Old answer: For some reason, the macro definition

#define INADDR_LOOPBACK         (u_int32_t)0x7f000001

from <netinet/in.h> is not imported into Swift. The problem might be the (u_int32_t) cast, because other macros like

#define INADDR_NONE             0xffffffff              /* -1 return */

are imported.

One solution is to define

let INADDR_LOOPBACK = UInt32(0x7f000001)

in your Swift code. Alternatively, add

#include <netinet/in.h>
const uint32_t kInAddrLoopback = INADDR_LOOPBACK;

to the bridging header file and use kInAddrLoopback in the Swift code. This is less error-prone because you don't have to repeat the constant.