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