@@ -50,11 +50,42 @@ extension URLCache {
50
50
open class CachedURLResponse : NSObject , NSSecureCoding , NSCopying {
51
51
52
52
public required init ? ( coder aDecoder: NSCoder ) {
53
- NSUnimplemented ( )
53
+ guard aDecoder. allowsKeyedCoding else {
54
+ /* Unkeyed unarchiving is not supported. */
55
+ return nil
56
+ }
57
+
58
+ guard let data = aDecoder. decodeObject ( of: NSData . self, forKey: " Data " ) else {
59
+ return nil
60
+ }
61
+ guard let response = aDecoder. decodeObject ( of: URLResponse . self, forKey: " URLResponse " ) else {
62
+ return nil
63
+ }
64
+ guard let storagePolicyValue = aDecoder. decodeObject ( of: NSNumber . self, forKey: " StoragePolicy " ) else {
65
+ return nil
66
+ }
67
+ guard let storagePolicy = URLCache . StoragePolicy ( rawValue: storagePolicyValue. uintValue) else {
68
+ return nil
69
+ }
70
+ let userInfo = aDecoder. decodeObject ( of: NSDictionary . self, forKey: " UserInfo " )
71
+
72
+ self . data = data as Data
73
+ self . response = response
74
+ self . storagePolicy = storagePolicy
75
+ self . userInfo = userInfo? . _swiftObject
54
76
}
55
77
56
78
open func encode( with aCoder: NSCoder ) {
57
- NSUnimplemented ( )
79
+ guard aCoder. allowsKeyedCoding else {
80
+ fatalError ( " We do not support saving to a non-keyed coder. " )
81
+ }
82
+
83
+ aCoder. encode ( data as NSData , forKey: " Data " )
84
+ aCoder. encode ( response, forKey: " URLResponse " )
85
+ aCoder. encode ( NSNumber ( value: storagePolicy. rawValue) , forKey: " StoragePolicy " )
86
+ if let userInfo = userInfo {
87
+ aCoder. encode ( userInfo. _nsObject, forKey: " UserInfo " )
88
+ }
58
89
}
59
90
60
91
static public var supportsSecureCoding : Bool {
@@ -66,7 +97,7 @@ open class CachedURLResponse : NSObject, NSSecureCoding, NSCopying {
66
97
}
67
98
68
99
open func copy( with zone: NSZone ? = nil ) -> Any {
69
- NSUnimplemented ( )
100
+ return self
70
101
}
71
102
72
103
/*!
@@ -81,7 +112,12 @@ open class CachedURLResponse : NSObject, NSSecureCoding, NSCopying {
81
112
corresponding to the given response.
82
113
@result an initialized CachedURLResponse.
83
114
*/
84
- public init ( response: URLResponse , data: Data ) { NSUnimplemented ( ) }
115
+ public init ( response: URLResponse , data: Data ) {
116
+ self . response = response. copy ( ) as! URLResponse
117
+ self . data = data
118
+ self . userInfo = nil
119
+ self . storagePolicy = . allowed
120
+ }
85
121
86
122
/*!
87
123
@method initWithResponse:data:userInfo:storagePolicy:
@@ -95,35 +131,69 @@ open class CachedURLResponse : NSObject, NSSecureCoding, NSCopying {
95
131
@param storagePolicy an URLCache.StoragePolicy constant.
96
132
@result an initialized CachedURLResponse.
97
133
*/
98
- public init ( response: URLResponse , data: Data , userInfo: [ AnyHashable : Any ] ? = [ : ] , storagePolicy: URLCache . StoragePolicy ) { NSUnimplemented ( ) }
134
+ public init ( response: URLResponse , data: Data , userInfo: [ AnyHashable : Any ] ? = nil , storagePolicy: URLCache . StoragePolicy ) {
135
+ self . response = response. copy ( ) as! URLResponse
136
+ self . data = data
137
+ self . userInfo = userInfo
138
+ self . storagePolicy = storagePolicy
139
+ }
99
140
100
141
/*!
101
142
@method response
102
143
@abstract Returns the response wrapped by this instance.
103
144
@result The response wrapped by this instance.
104
145
*/
105
- /*@NSCopying*/ open var response : URLResponse { NSUnimplemented ( ) }
146
+ /*@NSCopying*/ open private ( set ) var response : URLResponse
106
147
107
148
/*!
108
149
@method data
109
150
@abstract Returns the data of the receiver.
110
151
@result The data of the receiver.
111
152
*/
112
- /*@NSCopying*/ open var data : Data { NSUnimplemented ( ) }
153
+ /*@NSCopying*/ open private ( set ) var data : Data
113
154
114
155
/*!
115
156
@method userInfo
116
157
@abstract Returns the userInfo dictionary of the receiver.
117
158
@result The userInfo dictionary of the receiver.
118
159
*/
119
- open var userInfo : [ AnyHashable : Any ] ? { NSUnimplemented ( ) }
160
+ open private ( set ) var userInfo : [ AnyHashable : Any ] ?
120
161
121
162
/*!
122
163
@method storagePolicy
123
164
@abstract Returns the URLCache.StoragePolicy constant of the receiver.
124
165
@result The URLCache.StoragePolicy constant of the receiver.
125
166
*/
126
- open var storagePolicy : URLCache . StoragePolicy { NSUnimplemented ( ) }
167
+ open private( set) var storagePolicy : URLCache . StoragePolicy
168
+
169
+ open override func isEqual( _ value: Any ? ) -> Bool {
170
+ switch value {
171
+ case let other as CachedURLResponse :
172
+ return self . isEqual ( to: other)
173
+ default :
174
+ return false
175
+ }
176
+ }
177
+
178
+ private func isEqual( to other: CachedURLResponse ) -> Bool {
179
+ if self === other {
180
+ return true
181
+ }
182
+
183
+ // We cannot compare userInfo because of the values are Any, which
184
+ // doesn't conform to Equatable.
185
+ return self . response == other. response &&
186
+ self . data == other. data &&
187
+ self . storagePolicy == other. storagePolicy
188
+ }
189
+
190
+ open override var hash : Int {
191
+ var hasher = Hasher ( )
192
+ hasher. combine ( response)
193
+ hasher. combine ( data)
194
+ hasher. combine ( storagePolicy)
195
+ return hasher. finalize ( )
196
+ }
127
197
}
128
198
129
199
open class URLCache : NSObject {
0 commit comments