Structure
IPv4Address
public struct IPv4Address
A 32-bit numerical identifier assigned to a device on an Internet Protocol, version 4 network.
Relationships
Conforms To
Codable
Equatable
Hashable
LosslessStringConvertible
Nested Type Aliases
Octets
public typealias Octets = (UInt8, UInt8, UInt8, UInt8)
Initializers
init(octets:)
public init(octets: Octets = (0, 0, 0, 0))
Creates an address with the given octets.
init(value:_:)
public init(value: UInt32, _ octetArrangement: OctetArrangement)
Creates an address with the given 32-bit integer value.
Parameters
Name | Type | Description |
---|---|---|
value | UInt32 |
The integer value of the address. |
octetArrangement | OctetArrangement |
How the octets of
|
init(from:)
public init(from decoder: Decoder) throws
init?(_:)
@inlinable @inline(__always)
public init?<Source>(_ description: Source) where Source: StringProtocol
Parses an IPv4 address from a String.
The following formats are recognized:
-
a.b.c.d, where each numeric part defines the value of the address' octet at that position.
-
a.b.c, where a and b define the address' first 2 octets, and c is interpreted as a 16-bit integer whose most and least significant bytes define the address' 3rd and 4th octets respectively.
-
a.b, where a defines the address' first octet, and b is interpreted as a 24-bit integer whose bytes define the remaining octets from most to least significant.
-
a, where a is interpreted as a 32-bit integer whose bytes define the octets of the address in order from most to least significant.
The numeric parts may be written in decimal, octal (prefixed with a 0
), or hexadecimal (prefixed with 0x
, case-insensitive).
Additionally, a single trailing '.' is permitted (e.g. a.b.c.d.
).
Examples:
IPv4Address("0x7f.0.0.1")!.octets == (0x7f, 0x00, 0x00, 0x01) == "127.0.0.1"
IPv4Address("10.1.0x12.")!.octets == (0x0a, 0x01, 0x00, 0x12) == "10.1.0.18"
IPv4Address("0300.0xa80032")!.octets == (0xc0, 0xa8, 0x00, 0x32) == "192.168.0.50"
IPv4Address("0x8Badf00d")!.octets == (0x8b, 0xad, 0xf0, 0x0d) == "139.173.240.13"
Parameters
Name | Type | Description |
---|---|---|
description | Source |
The string to parse. |
init?(utf8:)
@inlinable @inline(__always)
public init?<UTF8Bytes>(utf8: UTF8Bytes) where UTF8Bytes: Collection, UTF8Bytes.Element == UInt8
Parses an IPv4 address from the given collection of UTF-8 code-units.
The following formats are recognized:
-
a.b.c.d, where each numeric part defines the value of the address' octet at that position.
-
a.b.c, where a and b define the address' first 2 octets, and c is interpreted as a 16-bit integer whose most and least significant bytes define the address' 3rd and 4th octets respectively.
-
a.b, where a defines the address' first octet, and b is interpreted as a 24-bit integer whose bytes define the remaining octets from most to least significant.
-
a, where a is interpreted as a 32-bit integer whose bytes define the octets of the address in order from most to least significant.
The numeric parts may be written in decimal, octal (prefixed with a 0
), or hexadecimal (prefixed with 0x
, case-insensitive).
Additionally, a single trailing '.' is permitted (e.g. a.b.c.d.
).
Examples:
IPv4Address("0x7f.0.0.1".utf8)!.octets == (0x7f, 0x00, 0x00, 0x01) == "127.0.0.1"
IPv4Address("10.1.0x12.".utf8)!.octets == (0x0a, 0x01, 0x00, 0x12) == "10.1.0.18"
IPv4Address("0300.0xa80032".utf8)!.octets == (0xc0, 0xa8, 0x00, 0x32) == "192.168.0.50"
IPv4Address("0x8Badf00d".utf8)!.octets == (0x8b, 0xad, 0xf0, 0x0d) == "139.173.240.13"
Parameters
Name | Type | Description |
---|---|---|
utf8 | UTF8Bytes |
The string to parse, as a collection of UTF-8 code-units. |
init?(dottedDecimal:)
@inlinable
public init?<S>(dottedDecimal string: S) where S: StringProtocol
Parses an IPv4 address from a String.
This simplified parser only recognises the 4-piece decimal notation ("a.b.c.d"), also known as dotted-decimal notation.
Parameters
Name | Type | Description |
---|---|---|
string | S |
The string to parse. |
init?(dottedDecimalUTF8:)
@inlinable
public init?<UTF8Bytes>(dottedDecimalUTF8 utf8: UTF8Bytes) where UTF8Bytes: Collection, UTF8Bytes.Element == UInt8
Parses an IPv4 address from a buffer of UTF-8 codeunits.
This simplified parser only recognises the 4-piece decimal notation ("a.b.c.d"), also known as dotted-decimal notation.
Parameters
Name | Type | Description |
---|---|---|
utf8 | UTF8Bytes |
The string to parse, as a collection of UTF-8 code-units. |
Properties
octets
public var octets: Octets
The octets of this address.
description
public var description: String
serialized
public var serialized: String
The textual representation of this address, in dotted decimal notation, as defined by RFC 4001.
Methods
hash(into:)
@inlinable
public func hash(into hasher: inout Hasher)
encode(to:)
public func encode(to encoder: Encoder) throws
Operators
==
@inlinable
public static func == (lhs: Self, rhs: Self) -> Bool