我正在尝试在swift中实现以下代码。但是我的i
变量拒绝与我的MAXADDRS交谈。它在Swift中说binary operator < cannot be applied to Clong
。如果我使用CInt
问题就会消失,但是当我在i
theAddr = ip_addrs[i]
上收到错误
InitAddresses();
GetIPAddresses();
GetHWAddresses();
var i = CLong()
var deviceIP = NSString()
for (i=0; i < MAXADDRS; ++i)
{
var localHost = 0x7F000001; // 127.0.0.1
var theAddr = CLong()
theAddr = ip_addrs[i]
if (theAddr == 0) {return}
if (theAddr == localHost){continue}
NSLog("Name: %s MAC: %s IP: %s\n", if_names[i], hw_addrs[i], ip_names[i]);
//decided what adapter you want details for
if (strncmp(if_names[i], "en", 2) == 0)
{
NSLog("Adapter en has a IP of %s", ip_names[i]);
}
}
// Do any additional setup after loading the view, typically from a nib.
}
它打算比较的MAXADDRS与以下OBC标题相关
这里的源文件
http://www.chrisandtennille.com/code/IPAddress.h http://www.chrisandtennille.com/code/IPAddress.c
我的桥接标题
#include "IPAddress.h"
#include "IPAddress.c"
答案 0 :(得分:1)
#define MAXADDRS 32
以
的形式导入Swiftpublic var MAXADDRS: Int32 { get }
另一方面,CLong
是Int
的别名(“C'long'类型。”)
因此,您需要将所有值转换为通用类型。以来
数组下标需要Int
索引,转换MAXADDRS
到Int
可能是最简单的解决方案:
var i = 0 // Int
for (i=0; i < Int(MAXADDRS); ++i) {
}
或更简单:
for i in 0 ..< Int(MAXADDRS) {
}