Skip to content

implementation for NSURLComponents.copy(with:) #461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions Foundation/NSURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -929,9 +929,49 @@ open class NSURLComponents: NSObject, NSCopying {
open override func copy() -> Any {
return copy(with: nil)
}


open override func isEqual(_ object: Any?) -> Bool {
if let other = object as? NSURLComponents {
if scheme != other.scheme {
return false
}
if user != other.user {
return false
}
if password != other.password {
return false
}
if host != other.host {
return false
}
if port != other.port {
return false
}
if path != other.path {
return false
}
if query != other.query {
return false
}
if fragment != other.fragment {
return false
}
return true
}
return false
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CFURLComponentsRef implements an equality checking function. Can we just use CFEqual?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to use it. But, couldn't figure out how to cast CFURLComponentsRef to CFTypeRef thay CFEqual expect. NSURLComponent doesn't support toll-free bridge.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NSURLComponents is a has-a not an is-a class so for subclasses this approach is by far safer imho


open func copy(with zone: NSZone? = nil) -> Any {
NSUnimplemented()
let copy = NSURLComponents()
copy.scheme = self.scheme
copy.user = self.user
copy.password = self.password
copy.host = self.host
copy.port = self.port
copy.path = self.path
copy.query = self.query
copy.fragment = self.fragment
return copy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CFURLComponentsRef also has _CFURLComponentsCreateCopy. Can we just use that and set the CFURLComponentsRef ivar directly?

Copy link
Contributor Author

@mbvreddy mbvreddy Jul 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parkera

_components is set in init's. There is no variant of init that takes CFURLComponentsRef.

So, I tried creating NSURLComponents using default init() and updated _components with _CFURLComponentsCreateCopy (after changing _components to var). But, this results in memory leak.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again the same issue above applies to subclassers; @mbvreddy's approach is correct for the abstract implementation.

}

// 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.
Expand Down
13 changes: 13 additions & 0 deletions TestFoundation/TestNSURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ class TestNSURLComponents : XCTestCase {
("test_string", test_string),
("test_port", test_portSetter),
("test_url", test_url),
("test_copy", test_copy)
]
}

Expand Down Expand Up @@ -486,4 +487,16 @@ class TestNSURLComponents : XCTestCase {
aURL = compWithoutAuthority.url(relativeTo: baseURL)
XCTAssertNil(aURL) //must be nil
}

func test_copy() {
let urlString = "https://www.swift.org/path/to/file.html?id=name"
let urlComponent = NSURLComponents(string: urlString)!
let copy = urlComponent.copy() as! NSURLComponents

/* Assert that NSURLComponents.copy did not return self */
XCTAssertFalse(copy === urlComponent)

/* Assert that NSURLComponents.copy is actually a copy of NSURLComponents */
XCTAssertTrue(copy.isEqual(urlComponent))
}
}