RVS_IPAddress
public protocol RVS_IPAddress
This protocol is an abstract base for IP addresses. When we parse a String as an IP address, it will return a specific implementation of this.
When we say “valid,” we don’t mean the address resolves to anything; just that it is syntactically correct.
Its principal use is to take a string that was generated by something like a text entry, and establish a proper IP address from that string. It’s a smart parser.
What Problem Does This Solve?
This was designed to allow us to simply represent IP addresses properly. This was mainly due to the complex nature of IPv6 addressing; with its “shortcut” syntax, representing contiguous runs of zeroes.
This class reads these, and will also give you properly formatted (and shortened) strings.
Usage
Start With A String
The struct can be instantiated by supplying a String, with a valid representation of either an IPv4 IP address, or an IPv6 IP address.
The struct will parse the String, and will store an internal Array of Ints, representing that IP address.
It will then be able to return the IP address in whatever format is required.
It validates the correctness of the address before storing it, so you know that the address is always in a valid form.
It does not verify that the address actually leads anywhere. It merely ensures that it is a correctly-formed IP address.
You can submit any string, and the factory method will ensure that an instance of the correct IP version handler is generated.
Example
if let testIP = RVS_IPAddressExtractIPAddress("1.2.3.4") {
print("This should be an IPv4 Address: \(String(describing: testIP))")
}
if let testIP = RVS_IPAddressExtractIPAddress("1:2:3:4:5:6:7:8") {
print("This should be an IPv6 Address: \(String(describing: testIP))")
}
You can also add TCP ports to the address:
if let testIP = RVS_IPAddressExtractIPAddress("1.2.3.4:5") {
print("This should be an IPv4 Address: \(String(describing: testIP))")
}
if let testIP = RVS_IPAddressExtractIPAddress("[1:2:3:4:5:6:7:8]:9") {
print("This should be an IPv6 Address: \(String(describing: testIP))")
}
The resulting addresses can be accessed as Arrays of Int, or as String, either with or without the TCP port.
-
isEmptyDefault implementationDefault Implementation
Declaration
Swift
var isEmpty: Bool { get }Return Value
true, if the IP address is invalid (all 0’s).
-
isV6Default implementationDefault Implementation
Declaration
Swift
var isV6: Bool { get }Return Value
true, if this is a valid IPV6 address.
-
isValidAddressDefault implementationDefault Implementation
Declaration
Swift
var isValidAddress: Bool { get }Return Value
true, if this is a valid IPV6 or IPV4 address.
-
addressDefault implementationDefault Implementation
Declaration
Swift
var address: String { get }Return Value
just the address portion of an address/port pair, as a syntactically correct String.
-
Declaration
Swift
var addressAndPort: String { get }Return Value
both the address, and the port, as a syntactically correct String.
-
Declaration
Swift
var port: Int { get set }Return Value
The TCP port, as an unsigned integer.
-
Declaration
Swift
var addressArray: [Int] { get set }Return Value
The actual IP address, as an Array of Int, with each element being one of the displayed elements.
-
descriptionDefault implementationDefault Implementation
Declaration
Swift
var description: String { get }Return Value
The address and port, as a String.
View on GitHub