Skip to content

Commit 87fba81

Browse files
author
Sergey Minakov
committed
[NSURLQueryItem] NSCoding Implementation
[NSURLQueryItem] Added tests for NSCoding [NSURLQueryItem] NSCoder implementation [NSURLQueryItem] 'isEqual' overriden [Docs] NSURLQueryItem status update
1 parent 1db5065 commit 87fba81

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

Diff for: Docs/Status.md

+1-1
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 | `NSCoding` remains unimplemented |
5959
| `NSHTTPURLResponse` | Mostly Complete | Substantial | `NSCoding` remains unimplemented |
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 | |

Diff for: Foundation/NSURL.swift

+25-2
Original file line numberDiff line numberDiff line change
@@ -910,11 +910,34 @@ open class NSURLQueryItem : NSObject, NSSecureCoding, NSCopying {
910910
}
911911

912912
required public init?(coder aDecoder: NSCoder) {
913-
NSUnimplemented()
913+
guard aDecoder.allowsKeyedCoding else {
914+
preconditionFailure("Unkeyed coding is unsupported.")
915+
}
916+
917+
let encodedName = aDecoder.decodeObject(forKey: "NS.name") as! NSString
918+
self.name = encodedName._swiftObject
919+
920+
let encodedValue = aDecoder.decodeObject(forKey: "NS.value") as? NSString
921+
self.value = encodedValue?._swiftObject
914922
}
915923

916924
open func encode(with aCoder: NSCoder) {
917-
NSUnimplemented()
925+
guard aCoder.allowsKeyedCoding else {
926+
preconditionFailure("Unkeyed coding is unsupported.")
927+
}
928+
929+
aCoder.encode(self.name._bridgeToObjectiveC(), forKey: "NS.name")
930+
aCoder.encode(self.value?._bridgeToObjectiveC(), forKey: "NS.value")
931+
}
932+
933+
open override func isEqual(_ object: Any?) -> Bool {
934+
if let other = object as? NSURLQueryItem {
935+
return other === self
936+
|| (other.name == self.name
937+
&& other.value == self.value)
938+
}
939+
940+
return false
918941
}
919942

920943
open let name: String

Diff for: TestFoundation/TestNSURL.swift

+8-1
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

@@ -413,6 +414,12 @@ class TestNSURL : XCTestCase {
413414
let queryItemCopy = queryItem.copy() as! NSURLQueryItem
414415
XCTAssertTrue(queryItem.isEqual(queryItemCopy))
415416
}
417+
418+
func test_itemNSCoding() {
419+
let queryItemA = NSURLQueryItem(name: "id", value: "23")
420+
let queryItemB = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: queryItemA)) as! NSURLQueryItem
421+
XCTAssertEqual(queryItemA, queryItemB, "Archived then unarchived query item must be equal.")
422+
}
416423
}
417424

418425
class TestNSURLComponents : XCTestCase {

0 commit comments

Comments
 (0)