0

I am trying to implement the following code in swift. But my i variable refuse to talk to my MAXADDRS. It says binary operator < cannot be applied to Clong in Swift. If I use CInt the problem goes away, but then I get an error on the variable i when assiginin 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.
        }

The MAXADDRS it intends to compare relates to the following OBC header

Source files here

http://www.chrisandtennille.com/code/IPAddress.h http://www.chrisandtennille.com/code/IPAddress.c

My bridging header

#include "IPAddress.h"
#include "IPAddress.c"
GuiSoySauce
  • 1,763
  • 3
  • 24
  • 37

1 Answers1

1
#define MAXADDRS    32

is imported to Swift as

public var MAXADDRS: Int32 { get }

On the other hand, CLong is an alias for Int ("The C 'long' type.") Therefore you need to convert all values to a common type. Since array subscripting requires an Int index, converting MAXADDRS to Int might be the easiest solution:

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

}

or more simply:

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

}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • If I do that then I get an error on `theAddr = ip_addrs[i]` saying Type (Uint) has no subscript members. – GuiSoySauce Nov 30 '15 at 21:29
  • @GuiSoySauce: That is actually a different problem, because a fixed size C array like `extern unsigned long ip_addrs[MAXADDRS];` is imported to Swift as a *tuple* `public var ip_addrs: (UInt, ..., UInt)`, and tuples cannot be subscripted like arrays (which is quite inconvenient). I figured out some "hack" to a similar problem in http://stackoverflow.com/questions/27455773/converting-a-c-char-array-to-a-string. There are other solutions using `Mirror`. – Martin R Nov 30 '15 at 21:38
  • @GuiSoySauce: Most (all?) of that stuff can be done in Swift natively, see http://stackoverflow.com/a/30754194/1187415 for an example. – Martin R Nov 30 '15 at 21:52
  • Hi Martin, Thanks for the reply. I did implement this one tackoverflow.com/a/30754194/1187415 and it works. The only thing is that it do not get the mac address of the devices. And I need both IP and MAc. If I could tweak this code to get MACs would work fine. Is it possible? – GuiSoySauce Nov 30 '15 at 21:57
  • @GuiSoySauce: I *assume* that it is possible but I don't have a ready solution. I would recommend do ask a new question if you don't find a solution, as this is now quite a different problem. – Martin R Nov 30 '15 at 22:25
  • Thanks @Martin I've created a new question at http://stackoverflow.com/questions/34009890/manage-ifaddrs-to-return-mac-addresses-as-well-in-swift – GuiSoySauce Nov 30 '15 at 23:28
  • gave you an up vote for the effort on helping me out. I appreciate. – GuiSoySauce Nov 30 '15 at 23:29
  • @GuiSoySauce: To be honest, I think that I have answered your initial question :) – Martin R Nov 30 '15 at 23:47