From a4c3a3521a1737cd827d2e2ec2521e2443baaba4 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 2 Aug 2018 12:31:15 +0100 Subject: [PATCH] NSURLComponents.hash: Implement --- Foundation/NSURL.swift | 13 +++++++++++++ TestFoundation/TestURL.swift | 11 ++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Foundation/NSURL.swift b/Foundation/NSURL.swift index f519981133..a6ffb2bd6c 100644 --- a/Foundation/NSURL.swift +++ b/Foundation/NSURL.swift @@ -983,6 +983,19 @@ open class NSURLComponents: NSObject, NSCopying { && fragment == other.fragment) } + open override var hash: Int { + var hasher = Hasher() + hasher.combine(scheme) + hasher.combine(user) + hasher.combine(password) + hasher.combine(host) + hasher.combine(port) + hasher.combine(path) + hasher.combine(query) + hasher.combine(fragment) + return hasher.finalize() + } + open func copy(with zone: NSZone? = nil) -> Any { let copy = NSURLComponents() copy.scheme = self.scheme diff --git a/TestFoundation/TestURL.swift b/TestFoundation/TestURL.swift index bad7a0ad47..d0783ec07b 100644 --- a/TestFoundation/TestURL.swift +++ b/TestFoundation/TestURL.swift @@ -521,6 +521,7 @@ class TestURLComponents : XCTestCase { ("test_port", test_portSetter), ("test_url", test_url), ("test_copy", test_copy), + ("test_hash", test_hash), ("test_createURLWithComponents", test_createURLWithComponents), ("test_path", test_path), ("test_percentEncodedPath", test_percentEncodedPath), @@ -615,7 +616,15 @@ class TestURLComponents : XCTestCase { /* Assert that NSURLComponents.copy is actually a copy of NSURLComponents */ XCTAssertTrue(copy.isEqual(urlComponent)) } - + + func test_hash() { + let c1 = URLComponents(string: "https://www.swift.org/path/to/file.html?id=name")! + let c2 = URLComponents(string: "https://www.swift.org/path/to/file.html?id=name")! + + XCTAssertEqual(c1, c2) + XCTAssertEqual(c1.hashValue, c2.hashValue) + } + func test_createURLWithComponents() { let urlComponents = NSURLComponents() urlComponents.scheme = "https";