Skip to content

Commit 55e6883

Browse files
committed
implementation for NSURLComponents.copy(with:)
1 parent 686d9b6 commit 55e6883

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
@@ -858,9 +858,49 @@ public class NSURLComponents: NSObject, NSCopying {
858858
public override func copy() -> AnyObject {
859859
return copy(with: nil)
860860
}
861-
861+
862+
public override func isEqual(_ object: AnyObject?) -> Bool {
863+
if let other = object as? NSURLComponents {
864+
if scheme != other.scheme {
865+
return false
866+
}
867+
if user != other.user {
868+
return false
869+
}
870+
if password != other.password {
871+
return false
872+
}
873+
if host != other.host {
874+
return false
875+
}
876+
if port != other.port {
877+
return false
878+
}
879+
if path != other.path {
880+
return false
881+
}
882+
if query != other.query {
883+
return false
884+
}
885+
if fragment != other.fragment {
886+
return false
887+
}
888+
return true
889+
}
890+
return false
891+
}
892+
862893
public func copy(with zone: NSZone? = nil) -> AnyObject {
863-
NSUnimplemented()
894+
let copy = NSURLComponents()
895+
copy.scheme = self.scheme
896+
copy.user = self.user
897+
copy.password = self.password
898+
copy.host = self.host
899+
copy.port = self.port
900+
copy.path = self.path
901+
copy.query = self.query
902+
copy.fragment = self.fragment
903+
return copy
864904
}
865905

866906
// 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
@@ -466,6 +466,7 @@ class TestNSURLComponents : XCTestCase {
466466
("test_string", test_string),
467467
("test_port", test_portSetter),
468468
("test_URLRelativeToURL", test_URLRelativeToURL),
469+
("test_copy", test_copy)
469470
]
470471
}
471472

@@ -531,4 +532,16 @@ class TestNSURLComponents : XCTestCase {
531532
aURL = compWithoutAuthority.url(relativeTo: baseURL)
532533
XCTAssertNil(aURL) //must be nil
533534
}
535+
536+
func test_copy() {
537+
let urlString = "https://www.swift.org/path/to/file.html?id=name"
538+
let urlComponent = NSURLComponents(string: urlString)!
539+
let copy = urlComponent.copy() as! NSURLComponents
540+
541+
/* Assert that NSURLComponents.copy did not return self */
542+
XCTAssertFalse(copy === urlComponent)
543+
544+
/* Assert that NSURLComponents.copy is actually a copy of NSURLComponents */
545+
XCTAssertTrue(copy.isEqual(urlComponent))
546+
}
534547
}

0 commit comments

Comments
 (0)