Return optional from String/IPv4 conversion
This commit is contained in:
parent
d83584e428
commit
6ffdcec47b
|
@ -104,10 +104,10 @@ public class DNSResolver {
|
|||
return "\(a).\(b).\(c).\(d)"
|
||||
}
|
||||
|
||||
public static func ipv4(fromString string: String) -> UInt32 {
|
||||
public static func ipv4(fromString string: String) -> UInt32? {
|
||||
let comps = string.components(separatedBy: ".")
|
||||
guard comps.count == 4 else {
|
||||
preconditionFailure()
|
||||
return nil
|
||||
}
|
||||
var ipv4: UInt32 = 0
|
||||
var bits: UInt32 = 0
|
||||
|
|
|
@ -43,7 +43,7 @@ class DNSTests: XCTestCase {
|
|||
XCTAssertEqual(DNSResolver.ipv4(fromString: addr), ip)
|
||||
XCTAssertEqual(DNSResolver.string(fromIPv4: ip), addr)
|
||||
|
||||
XCTAssertEqual(DNSResolver.string(fromIPv4: DNSResolver.ipv4(fromString: addr)), addr)
|
||||
XCTAssertEqual(DNSResolver.string(fromIPv4: DNSResolver.ipv4(fromString: addr)!), addr)
|
||||
XCTAssertEqual(DNSResolver.ipv4(fromString: DNSResolver.string(fromIPv4: ip)), ip)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue