INADDR_LOOPBACK macro from <netinet/in.h> not imported in swift

末鹿安然 提交于 2020-01-06 21:21:40

问题


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


回答1:


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.




回答2:


From the Apple documentation.

Declare simple macros as global constants, and translate complex macros into functions.



来源:https://stackoverflow.com/questions/34042017/inaddr-loopback-macro-from-netinet-in-h-not-imported-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!