Skip to content

[NSURLQueryItem] NSCoding Implementation #712

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
Dec 15, 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
2 changes: 1 addition & 1 deletion Docs/Status.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ There is no _Complete_ status for test coverage because there are always additio
| `URLResponse` | Mostly Complete | Incomplete | `NSCoding` remains unimplemented |
| `NSHTTPURLResponse` | Mostly Complete | Substantial | `NSCoding` remains unimplemented |
| `NSURL` | Mostly Complete | Substantial | `NSCoding` with non-keyed-coding archivers, `checkResourceIsReachable()`, and resource values remain unimplemented |
| `NSURLQueryItem` | Mostly Complete | N/A | `NSCoding` remains unimplemented |
| `NSURLQueryItem` | Mostly Complete | N/A | |
| `URLResourceKey` | Complete | N/A | |
| `URLFileResourceType` | Complete | N/A | |
| `URL` | Complete | Incomplete | |
Expand Down
27 changes: 25 additions & 2 deletions Foundation/NSURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -910,11 +910,34 @@ open class NSURLQueryItem : NSObject, NSSecureCoding, NSCopying {
}

required public init?(coder aDecoder: NSCoder) {
NSUnimplemented()
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}

let encodedName = aDecoder.decodeObject(forKey: "NS.name") as! NSString
self.name = encodedName._swiftObject

let encodedValue = aDecoder.decodeObject(forKey: "NS.value") as? NSString
self.value = encodedValue?._swiftObject
}

open func encode(with aCoder: NSCoder) {
NSUnimplemented()
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}

aCoder.encode(self.name._bridgeToObjectiveC(), forKey: "NS.name")
aCoder.encode(self.value?._bridgeToObjectiveC(), forKey: "NS.value")
}

open override func isEqual(_ object: Any?) -> Bool {
if let other = object as? NSURLQueryItem {
return other === self
|| (other.name == self.name
&& other.value == self.value)
}

return false
}

open let name: String
Expand Down
9 changes: 8 additions & 1 deletion TestFoundation/TestNSURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class TestNSURL : XCTestCase {
("test_fileURLWithPath", test_fileURLWithPath),
("test_fileURLWithPath_isDirectory", test_fileURLWithPath_isDirectory),
("test_URLByResolvingSymlinksInPath", test_URLByResolvingSymlinksInPath),
("test_copy", test_copy)
("test_copy", test_copy),
("test_itemNSCoding", test_itemNSCoding),
]
}

Expand Down Expand Up @@ -413,6 +414,12 @@ class TestNSURL : XCTestCase {
let queryItemCopy = queryItem.copy() as! NSURLQueryItem
XCTAssertTrue(queryItem.isEqual(queryItemCopy))
}

func test_itemNSCoding() {
let queryItemA = NSURLQueryItem(name: "id", value: "23")
let queryItemB = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: queryItemA)) as! NSURLQueryItem
XCTAssertEqual(queryItemA, queryItemB, "Archived then unarchived query item must be equal.")
}
}

class TestNSURLComponents : XCTestCase {
Expand Down