Skip to content

Commit 099f97a

Browse files
mbvreddyparkera
authored andcommitted
implementation for NSURLComponents.copy(with:) (#461)
change AnyObject to Any
1 parent 12c8461 commit 099f97a

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

Foundation/NSURL.swift

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,9 +929,49 @@ open class NSURLComponents: NSObject, NSCopying {
929929
open override func copy() -> Any {
930930
return copy(with: nil)
931931
}
932-
932+
933+
open override func isEqual(_ object: Any?) -> Bool {
934+
if let other = object as? NSURLComponents {
935+
if scheme != other.scheme {
936+
return false
937+
}
938+
if user != other.user {
939+
return false
940+
}
941+
if password != other.password {
942+
return false
943+
}
944+
if host != other.host {
945+
return false
946+
}
947+
if port != other.port {
948+
return false
949+
}
950+
if path != other.path {
951+
return false
952+
}
953+
if query != other.query {
954+
return false
955+
}
956+
if fragment != other.fragment {
957+
return false
958+
}
959+
return true
960+
}
961+
return false
962+
}
963+
933964
open func copy(with zone: NSZone? = nil) -> Any {
934-
NSUnimplemented()
965+
let copy = NSURLComponents()
966+
copy.scheme = self.scheme
967+
copy.user = self.user
968+
copy.password = self.password
969+
copy.host = self.host
970+
copy.port = self.port
971+
copy.path = self.path
972+
copy.query = self.query
973+
copy.fragment = self.fragment
974+
return copy
935975
}
936976

937977
// Initialize a NSURLComponents with the components of a URL. If resolvingAgainstBaseURL is YES and url is a relative URL, the components of [url absoluteURL] are used. If the url string from the NSURL is malformed, nil is returned.

TestFoundation/TestNSURL.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ class TestNSURLComponents : XCTestCase {
421421
("test_string", test_string),
422422
("test_port", test_portSetter),
423423
("test_url", test_url),
424+
("test_copy", test_copy)
424425
]
425426
}
426427

@@ -486,4 +487,16 @@ class TestNSURLComponents : XCTestCase {
486487
aURL = compWithoutAuthority.url(relativeTo: baseURL)
487488
XCTAssertNil(aURL) //must be nil
488489
}
490+
491+
func test_copy() {
492+
let urlString = "https://www.swift.org/path/to/file.html?id=name"
493+
let urlComponent = NSURLComponents(string: urlString)!
494+
let copy = urlComponent.copy() as! NSURLComponents
495+
496+
/* Assert that NSURLComponents.copy did not return self */
497+
XCTAssertFalse(copy === urlComponent)
498+
499+
/* Assert that NSURLComponents.copy is actually a copy of NSURLComponents */
500+
XCTAssertTrue(copy.isEqual(urlComponent))
501+
}
489502
}

0 commit comments

Comments
 (0)