Skip to content

Commit c5498b3

Browse files
authored
Merge pull request #712 from naithar/NSURLQueryItem-NSCoding
2 parents afd53fc + 87fba81 commit c5498b3

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

Docs/Status.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ There is no _Complete_ status for test coverage because there are always additio
5858
| `URLResponse` | Mostly Complete | Incomplete | |
5959
| `NSHTTPURLResponse` | Mostly Complete | Substantial | |
6060
| `NSURL` | Mostly Complete | Substantial | `NSCoding` with non-keyed-coding archivers, `checkResourceIsReachable()`, and resource values remain unimplemented |
61-
| `NSURLQueryItem` | Mostly Complete | N/A | `NSCoding` remains unimplemented |
61+
| `NSURLQueryItem` | Mostly Complete | N/A | |
6262
| `URLResourceKey` | Complete | N/A | |
6363
| `URLFileResourceType` | Complete | N/A | |
6464
| `URL` | Complete | Incomplete | |

Foundation/NSURL.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,11 +919,34 @@ open class NSURLQueryItem : NSObject, NSSecureCoding, NSCopying {
919919
}
920920

921921
required public init?(coder aDecoder: NSCoder) {
922-
NSUnimplemented()
922+
guard aDecoder.allowsKeyedCoding else {
923+
preconditionFailure("Unkeyed coding is unsupported.")
924+
}
925+
926+
let encodedName = aDecoder.decodeObject(forKey: "NS.name") as! NSString
927+
self.name = encodedName._swiftObject
928+
929+
let encodedValue = aDecoder.decodeObject(forKey: "NS.value") as? NSString
930+
self.value = encodedValue?._swiftObject
923931
}
924932

925933
open func encode(with aCoder: NSCoder) {
926-
NSUnimplemented()
934+
guard aCoder.allowsKeyedCoding else {
935+
preconditionFailure("Unkeyed coding is unsupported.")
936+
}
937+
938+
aCoder.encode(self.name._bridgeToObjectiveC(), forKey: "NS.name")
939+
aCoder.encode(self.value?._bridgeToObjectiveC(), forKey: "NS.value")
940+
}
941+
942+
open override func isEqual(_ object: Any?) -> Bool {
943+
if let other = object as? NSURLQueryItem {
944+
return other === self
945+
|| (other.name == self.name
946+
&& other.value == self.value)
947+
}
948+
949+
return false
927950
}
928951

929952
open let name: String

TestFoundation/TestNSURL.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ class TestNSURL : XCTestCase {
6161
("test_fileURLWithPath", test_fileURLWithPath),
6262
("test_fileURLWithPath_isDirectory", test_fileURLWithPath_isDirectory),
6363
("test_URLByResolvingSymlinksInPath", test_URLByResolvingSymlinksInPath),
64-
("test_copy", test_copy)
64+
("test_copy", test_copy),
65+
("test_itemNSCoding", test_itemNSCoding),
6566
]
6667
}
6768

@@ -431,6 +432,12 @@ class TestNSURL : XCTestCase {
431432
let queryItemCopy = queryItem.copy() as! NSURLQueryItem
432433
XCTAssertTrue(queryItem.isEqual(queryItemCopy))
433434
}
435+
436+
func test_itemNSCoding() {
437+
let queryItemA = NSURLQueryItem(name: "id", value: "23")
438+
let queryItemB = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: queryItemA)) as! NSURLQueryItem
439+
XCTAssertEqual(queryItemA, queryItemB, "Archived then unarchived query item must be equal.")
440+
}
434441
}
435442

436443
class TestNSURLComponents : XCTestCase {

0 commit comments

Comments
 (0)