I need to convert IP address (e.g. "127.0.0.1") to integer value and vice-versa for my logger. I’ve found some samples for ObjC:
- How to convert an IP address from NSString to unsigned int in Objective-C?
- iOS convert IP Address to integer and backwards
How to do it in Swift and what the best way?
2
Answers
There two possible approaches to do it in Swift:
inet_aton
andinet_ntoa
IPv4Address
fromNetwork
How to use:
This is how I would approach the conversion. It might look a bit over engineering but all properties are useful in other contexts as well:
The following implementations rely on Network Technology:
Usage: