Return optional from String/IPv4 conversion

This commit is contained in:
Davide De Rosa 2018-09-19 18:10:40 +02:00
parent d83584e428
commit 6ffdcec47b
2 changed files with 3 additions and 3 deletions

View File

@ -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

View File

@ -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)
}
}