@@ -74,7 +74,7 @@ Since RxCocoa needs to automagically create those Proxys and because views that
74
74
*/
75
75
public protocol DelegateProxyType : class {
76
76
associatedtype ParentObject : AnyObject
77
- associatedtype Delegate : AnyObject
77
+ associatedtype Delegate
78
78
79
79
/// It is require that enumerate call `register` of the extended DelegateProxy subclasses here.
80
80
static func registerKnownImplementations( )
@@ -130,6 +130,25 @@ extension DelegateProxyType {
130
130
}
131
131
}
132
132
133
+ // workaround of Delegate: class
134
+ extension DelegateProxyType {
135
+ static func _currentDelegate( for object: ParentObject ) -> AnyObject ? {
136
+ return currentDelegate ( for: object) . map { $0 as AnyObject }
137
+ }
138
+
139
+ static func _setCurrentDelegate( _ delegate: AnyObject ? , to object: ParentObject ) {
140
+ return setCurrentDelegate ( castOptionalOrFatalError ( delegate) , to: object)
141
+ }
142
+
143
+ func _forwardToDelegate( ) -> AnyObject ? {
144
+ return forwardToDelegate ( ) . map { $0 as AnyObject }
145
+ }
146
+
147
+ func _setForwardToDelegate( _ forwardToDelegate: AnyObject ? , retainDelegate: Bool ) {
148
+ return setForwardToDelegate ( castOptionalOrFatalError ( forwardToDelegate) , retainDelegate: retainDelegate)
149
+ }
150
+ }
151
+
133
152
extension DelegateProxyType {
134
153
135
154
/// Store DelegateProxy subclass to factory.
@@ -168,8 +187,7 @@ extension DelegateProxyType {
168
187
169
188
let maybeProxy = self . assignedProxy ( for: object)
170
189
171
- // Type is ideally be `(Self & Delegate)`, but Swift 3.0 doesn't support it.
172
- let proxy : Delegate
190
+ let proxy : AnyObject
173
191
if let existingProxy = maybeProxy {
174
192
proxy = existingProxy
175
193
}
@@ -178,15 +196,15 @@ extension DelegateProxyType {
178
196
self . assignProxy ( proxy, toObject: object)
179
197
assert ( self . assignedProxy ( for: object) === proxy)
180
198
}
181
- let currentDelegate = self . currentDelegate ( for: object)
199
+ let currentDelegate = self . _currentDelegate ( for: object)
182
200
let delegateProxy : Self = castOrFatalError ( proxy)
183
201
184
202
if currentDelegate !== delegateProxy {
185
- delegateProxy. setForwardToDelegate ( currentDelegate, retainDelegate: false )
186
- assert ( delegateProxy. forwardToDelegate ( ) === currentDelegate)
187
- self . setCurrentDelegate ( proxy, to: object)
188
- assert ( self . currentDelegate ( for: object) === proxy)
189
- assert ( delegateProxy. forwardToDelegate ( ) === currentDelegate)
203
+ delegateProxy. _setForwardToDelegate ( currentDelegate, retainDelegate: false )
204
+ assert ( delegateProxy. _forwardToDelegate ( ) === currentDelegate)
205
+ self . _setCurrentDelegate ( proxy, to: object)
206
+ assert ( self . _currentDelegate ( for: object) === proxy)
207
+ assert ( delegateProxy. _forwardToDelegate ( ) === currentDelegate)
190
208
}
191
209
192
210
return delegateProxy
@@ -200,10 +218,10 @@ extension DelegateProxyType {
200
218
/// - parameter onProxyForObject: Object that has `delegate` property.
201
219
/// - returns: Disposable object that can be used to clear forward delegate.
202
220
public static func installForwardDelegate( _ forwardDelegate: Delegate , retainDelegate: Bool , onProxyForObject object: ParentObject ) -> Disposable {
203
- weak var weakForwardDelegate : AnyObject ? = forwardDelegate
221
+ weak var weakForwardDelegate : AnyObject ? = forwardDelegate as AnyObject
204
222
let proxy = self . proxy ( for: object)
205
223
206
- assert ( proxy. forwardToDelegate ( ) === nil , " This is a feature to warn you that there is already a delegate (or data source) set somewhere previously. The action you are trying to perform will clear that delegate (data source) and that means that some of your features that depend on that delegate (data source) being set will likely stop working. \n " +
224
+ assert ( proxy. _forwardToDelegate ( ) === nil , " This is a feature to warn you that there is already a delegate (or data source) set somewhere previously. The action you are trying to perform will clear that delegate (data source) and that means that some of your features that depend on that delegate (data source) being set will likely stop working. \n " +
207
225
" If you are ok with this, try to set delegate (data source) to `nil` in front of this operation. \n " +
208
226
" This is the source object value: \( object) \n " +
209
227
" This this the original delegate (data source) value: \( proxy. forwardToDelegate ( ) !) \n " +
@@ -216,7 +234,7 @@ extension DelegateProxyType {
216
234
217
235
let delegate : AnyObject ? = weakForwardDelegate
218
236
219
- assert ( delegate == nil || proxy. forwardToDelegate ( ) === delegate, " Delegate was changed from time it was first set. Current \( String ( describing: proxy. forwardToDelegate ( ) ) ) , and it should have been \( proxy) " )
237
+ assert ( delegate == nil || proxy. _forwardToDelegate ( ) === delegate, " Delegate was changed from time it was first set. Current \( String ( describing: proxy. forwardToDelegate ( ) ) ) , and it should have been \( proxy) " )
220
238
221
239
proxy. setForwardToDelegate ( nil , retainDelegate: retainDelegate)
222
240
}
@@ -225,27 +243,25 @@ extension DelegateProxyType {
225
243
226
244
227
245
// fileprivate extensions
228
- extension DelegateProxyType
229
- {
246
+ extension DelegateProxyType {
230
247
fileprivate static var factory : DelegateProxyFactory {
231
248
return DelegateProxyFactory . sharedFactory ( for: self )
232
249
}
233
250
234
-
235
- fileprivate static func assignedProxy( for object: ParentObject ) -> Delegate ? {
251
+ fileprivate static func assignedProxy( for object: ParentObject ) -> AnyObject ? {
236
252
let maybeDelegate = objc_getAssociatedObject ( object, self . identifier)
237
- return castOptionalOrFatalError ( maybeDelegate. map { $0 as AnyObject } )
253
+ return castOptionalOrFatalError ( maybeDelegate)
238
254
}
239
255
240
- fileprivate static func assignProxy( _ proxy: Delegate , toObject object: ParentObject ) {
256
+ fileprivate static func assignProxy( _ proxy: AnyObject , toObject object: ParentObject ) {
241
257
objc_setAssociatedObject ( object, self . identifier, proxy, . OBJC_ASSOCIATION_RETAIN)
242
258
}
243
259
}
244
260
245
261
/// Describes an object that has a delegate.
246
262
public protocol HasDelegate : AnyObject {
247
263
/// Delegate type
248
- associatedtype Delegate : AnyObject
264
+ associatedtype Delegate
249
265
250
266
/// Delegate
251
267
var delegate : Delegate ? { get set }
@@ -264,7 +280,7 @@ extension DelegateProxyType where ParentObject: HasDelegate, Self.Delegate == Pa
264
280
/// Describes an object that has a data source.
265
281
public protocol HasDataSource : AnyObject {
266
282
/// Data source type
267
- associatedtype DataSource : AnyObject
283
+ associatedtype DataSource
268
284
269
285
/// Data source
270
286
var dataSource : DataSource ? { get set }
@@ -286,7 +302,8 @@ extension DelegateProxyType where ParentObject: HasDataSource, Self.Delegate ==
286
302
extension ObservableType {
287
303
func subscribeProxyDataSource< DelegateProxy: DelegateProxyType > ( ofObject object: DelegateProxy . ParentObject , dataSource: DelegateProxy . Delegate , retainDataSource: Bool , binding: @escaping ( DelegateProxy , Event < E > ) -> Void )
288
304
-> Disposable
289
- where DelegateProxy. ParentObject: UIView {
305
+ where DelegateProxy. ParentObject: UIView
306
+ , DelegateProxy. Delegate: AnyObject {
290
307
let proxy = DelegateProxy . proxy ( for: object)
291
308
let unregisterDelegate = DelegateProxy . installForwardDelegate ( dataSource, retainDelegate: retainDataSource, onProxyForObject: object)
292
309
// this is needed to flush any delayed old state (https://github.com/RxSwiftCommunity/RxDataSources/pull/75)
@@ -368,8 +385,7 @@ extension DelegateProxyType where ParentObject: HasDataSource, Self.Delegate ==
368
385
_identifier = proxyType. identifier
369
386
}
370
387
371
- fileprivate func extend< DelegateProxy: DelegateProxyType , ParentObject> ( make: @escaping ( ParentObject ) -> DelegateProxy )
372
- {
388
+ fileprivate func extend< DelegateProxy: DelegateProxyType , ParentObject> ( make: @escaping ( ParentObject ) -> DelegateProxy ) {
373
389
MainScheduler . ensureExecutingOnScheduler ( )
374
390
precondition ( _identifier == DelegateProxy . identifier, " Delegate proxy has inconsistent identifier " )
375
391
precondition ( ( DelegateProxy . self as? DelegateProxy . Delegate) != nil , " DelegateProxy subclass should be as a Delegate " )
0 commit comments