【问题标题】:Binary operator < cannot be applied to Clong in Swift二元运算符 < 不能应用于 Swift 中的 Clong
【发布时间】:2016-03-04 15:34:27
【问题描述】:

我正在尝试快速实现以下代码。但我的i 变量拒绝与我的 MAXADDRS 对话。它在 Swift 中显示 binary operator &lt; cannot be applied to Clong。如果我使用CInt,问题就会消失,但是当assiginin theAddr = ip_addrs[i] 时,变量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"

【问题讨论】:

  • CLongInt(“C long 类型”)的(预定义)别名。
  • MAXADDRS 在哪里/如何定义?
  • @MartinR 谢谢 - Google 在我的研究中返回非常
  • 我已经用 MAXADDRS 更新了这个问题。我热衷于使用 Int 而不是 CLong。但后来我得到了不同的错误。

标签: swift


【解决方案1】:
#define MAXADDRS    32

被导入到 Swift 中

public var MAXADDRS: Int32 { get }

另一方面,CLongInt 的别名(“C 'long' 类型。”) 因此,您需要将所有值转换为通用类型。自从 数组下标需要Int 索引,转换MAXADDRSInt 可能是最简单的解决方案:

var i = 0 // Int
for (i=0; i < Int(MAXADDRS); ++i) {

}

或更简单地说:

for i in 0 ..< Int(MAXADDRS) {

}

【讨论】:

  • 如果我这样做了,我会在 theAddr = ip_addrs[i] 上收到一个错误,说 Type (Uint) has no subscript members。
  • @GuiSoySauce:这实际上是一个不同的问题,因为像extern unsigned long ip_addrs[MAXADDRS]; 这样的固定大小的 C 数组作为 tuple public var ip_addrs: (UInt, ..., UInt) 导入到 Swift 中,而元组不能像数组一样下标(这很不方便)。我在stackoverflow.com/questions/27455773/… 中发现了一些类似问题的“hack”。还有其他使用Mirror的解决方案。
  • @GuiSoySauce:大部分(全部?)这些东西都可以在原生 Swift 中完成,请参阅 stackoverflow.com/a/30754194/1187415 示例。
  • 嗨马丁,感谢您的回复。我确实实现了这个 tackoverflow.com/a/30754194/1187415 并且它有效。唯一的问题是它没有获取设备的mac地址。我需要 IP 和 MAC。如果我可以调整此代码以获取 MAC 将正常工作。有可能吗?
  • @GuiSoySauce:我假设这是可能的,但我没有现成的解决方案。如果您找不到解决方案,我建议您提出一个新问题,因为现在这是一个完全不同的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
  • 2015-08-21
  • 1970-01-01
  • 2015-08-26
  • 1970-01-01
相关资源
最近更新 更多