I have an Objective-C class which have
#include <arpa/inet.h>
But how to include this in Swift language?
I have an Objective-C class which have
#include <arpa/inet.h>
But how to include this in Swift language?
You can't include a C header directly in a Swift source code file. Instead include the C header in an Objective C header file, and add a bridging header that includes all the needed Objective C headers to the Xcode project.
Some of the functions defined in <arpa/inet.h>
are available if you import Foundation
or Darwin
.
However, understanding how to use them will take some work. You should create a playground or use the command-line REPL to experiment. Example:
$ xcrun swift
"crashlog" and "save_crashlog" command installed, use the "--help" option for detailed help
"malloc_info", "ptr_refs", "cstr_refs", and "objc_refs" commands have been installed, use the "--help" options on these commands for detailed help.
Welcome to Swift! Type :help for assistance.
1> import Foundation
2> inet_addr("10.0.1.1")
$R6: in_addr_t = {
value = 16842762
}
3> var a = in_addr(s_addr: 16842762)
a: in_addr = {
s_addr = {
value = 16842762
}
}
4> inet_netof(a)
$R7: in_addr_t = {
value = 10
}
5>
You will probably find that the functions that deal with C strings (like inet_ntoa
) are just too difficult to work with. It would probably be easier for you to just write a wrapper class in Objective-C that provides a class method for each <arpa/inet.h>
function you are interested in. The class methods can translate between raw C strings and NSString
s. Then you can call on the class methods of this Objective-C wrapper from your Swift code.