在委托字段未调用“delegate”时实现“DelegateProxy”(RxSwift/RxCocoa)
Implementing `DelegateProxy` (RxSwift/RxCocoa) when delegate field is not called `delegate`
我在 DelegateProxy
实施方面需要一些帮助。具体来说,当委托字段的名称与 delegate
不同时,正确的实现方式是什么?比如在SKPhysicsContactDelegate
里面叫做contactDelegate
。我尝试定义一个计算值 delegate
,但没有成功 - https://github.com/maxvol/RxSpriteKit/blob/master/Proxy/RxSKPhysicsContactDelegateProxy.swift
失败 "DelegateProxy has no factory of <PKPhysicsWorld: 0x280b18990>. Implement DelegateProxy subclass for <PKPhysicsWorld: 0x280b18990> first."
也许它甚至与委托字段名称无关,但这是我能找到的与正常工作的代理的唯一区别。
更新:哈!我刚刚注意到错误消息显示 PKPhysicsWorld
,而不是 SKPhysicsWorld
。所以我的假设是,它与 DelegateProxyFactory.createProxy
中的 object
是 PKPhysicsWorld
而不是 SKPhysicsWorld
和 _factories[ObjectIdentifier(mirror.subjectType)]
returns 这一事实有关nil
.
您收到该错误的原因是您的 registerKnownImplementations()
函数未获得 运行.
以下要点应该有效:https://gist.github.com/dtartaglia/9f1f937628504ca56dbb1aac7d91df2b
代码也在下面,但要点可以保持最新:
//
// SKPhysicsWorld+Rx.swift
//
// Created by Daniel Tartaglia on 21 Jan 2019.
// Copyright © 2019 Daniel Tartaglia. MIT License.
//
import RxSwift
import SpriteKit
public
extension Reactive where Base: SKPhysicsWorld {
var didBegin: Observable<SKPhysicsContact> {
return Observable.create { observer in
physicsContatctDelegatesLock.lock(); defer { physicsContatctDelegatesLock.unlock() }
let uuid = UUID()
if let (delegate, beginners, enders) = physicsContatctDelegates[self.base] {
var new = beginners
new[uuid] = observer
physicsContatctDelegates[self.base] = (delegate, new, enders)
}
else {
let delegate = PhysicsContactDelegate(for: self.base)
self.base.contactDelegate = delegate
physicsContatctDelegates[self.base] = (delegate, [uuid: observer], [:])
}
return Disposables.create {
physicsContatctDelegatesLock.lock(); defer { physicsContatctDelegatesLock.unlock() }
let (delegate, beginners, enders) = physicsContatctDelegates[self.base]!
var new = beginners
new.removeValue(forKey: uuid)
if new.isEmpty && enders.isEmpty {
physicsContatctDelegates.removeValue(forKey: self.base)
}
else {
physicsContatctDelegates[self.base] = (delegate, new, enders)
}
}
}
}
var didEnd: Observable<SKPhysicsContact> {
return Observable.create { observer in
physicsContatctDelegatesLock.lock(); defer { physicsContatctDelegatesLock.unlock() }
let uuid = UUID()
if let (delegate, beginners, enders) = physicsContatctDelegates[self.base] {
var new = enders
new[uuid] = observer
physicsContatctDelegates[self.base] = (delegate, beginners, new)
}
else {
let delegate = PhysicsContactDelegate(for: self.base)
self.base.contactDelegate = delegate
physicsContatctDelegates[self.base] = (delegate, [:], [uuid: observer])
}
return Disposables.create {
physicsContatctDelegatesLock.lock(); defer { physicsContatctDelegatesLock.unlock() }
let (delegate, beginners, enders) = physicsContatctDelegates[self.base]!
var new = enders
new.removeValue(forKey: uuid)
if new.isEmpty && enders.isEmpty {
physicsContatctDelegates.removeValue(forKey: self.base)
}
else {
physicsContatctDelegates[self.base] = (delegate, beginners, new)
}
}
}
}
}
private
class PhysicsContactDelegate: NSObject, SKPhysicsContactDelegate {
init(for world: SKPhysicsWorld) {
self.world = world
super.init()
}
func didBegin(_ contact: SKPhysicsContact) {
physicsContatctDelegatesLock.lock(); defer { physicsContatctDelegatesLock.unlock() }
let (_, beginners, _) = physicsContatctDelegates[world]!
for each in beginners.values {
each.onNext(contact)
}
}
func didEnd(_ contact: SKPhysicsContact) {
physicsContatctDelegatesLock.lock(); defer { physicsContatctDelegatesLock.unlock() }
let (_, _, enders) = physicsContatctDelegates[world]!
for each in enders.values {
each.onNext(contact)
}
}
let world: SKPhysicsWorld
}
private let physicsContatctDelegatesLock = NSRecursiveLock()
private var physicsContatctDelegates: [SKPhysicsWorld: (SKPhysicsContactDelegate, [UUID: AnyObserver<SKPhysicsContact>], [UUID: AnyObserver<SKPhysicsContact>])] = [:]
我在 DelegateProxy
实施方面需要一些帮助。具体来说,当委托字段的名称与 delegate
不同时,正确的实现方式是什么?比如在SKPhysicsContactDelegate
里面叫做contactDelegate
。我尝试定义一个计算值 delegate
,但没有成功 - https://github.com/maxvol/RxSpriteKit/blob/master/Proxy/RxSKPhysicsContactDelegateProxy.swift
失败 "DelegateProxy has no factory of <PKPhysicsWorld: 0x280b18990>. Implement DelegateProxy subclass for <PKPhysicsWorld: 0x280b18990> first."
也许它甚至与委托字段名称无关,但这是我能找到的与正常工作的代理的唯一区别。
更新:哈!我刚刚注意到错误消息显示 PKPhysicsWorld
,而不是 SKPhysicsWorld
。所以我的假设是,它与 DelegateProxyFactory.createProxy
中的 object
是 PKPhysicsWorld
而不是 SKPhysicsWorld
和 _factories[ObjectIdentifier(mirror.subjectType)]
returns 这一事实有关nil
.
您收到该错误的原因是您的 registerKnownImplementations()
函数未获得 运行.
以下要点应该有效:https://gist.github.com/dtartaglia/9f1f937628504ca56dbb1aac7d91df2b
代码也在下面,但要点可以保持最新:
//
// SKPhysicsWorld+Rx.swift
//
// Created by Daniel Tartaglia on 21 Jan 2019.
// Copyright © 2019 Daniel Tartaglia. MIT License.
//
import RxSwift
import SpriteKit
public
extension Reactive where Base: SKPhysicsWorld {
var didBegin: Observable<SKPhysicsContact> {
return Observable.create { observer in
physicsContatctDelegatesLock.lock(); defer { physicsContatctDelegatesLock.unlock() }
let uuid = UUID()
if let (delegate, beginners, enders) = physicsContatctDelegates[self.base] {
var new = beginners
new[uuid] = observer
physicsContatctDelegates[self.base] = (delegate, new, enders)
}
else {
let delegate = PhysicsContactDelegate(for: self.base)
self.base.contactDelegate = delegate
physicsContatctDelegates[self.base] = (delegate, [uuid: observer], [:])
}
return Disposables.create {
physicsContatctDelegatesLock.lock(); defer { physicsContatctDelegatesLock.unlock() }
let (delegate, beginners, enders) = physicsContatctDelegates[self.base]!
var new = beginners
new.removeValue(forKey: uuid)
if new.isEmpty && enders.isEmpty {
physicsContatctDelegates.removeValue(forKey: self.base)
}
else {
physicsContatctDelegates[self.base] = (delegate, new, enders)
}
}
}
}
var didEnd: Observable<SKPhysicsContact> {
return Observable.create { observer in
physicsContatctDelegatesLock.lock(); defer { physicsContatctDelegatesLock.unlock() }
let uuid = UUID()
if let (delegate, beginners, enders) = physicsContatctDelegates[self.base] {
var new = enders
new[uuid] = observer
physicsContatctDelegates[self.base] = (delegate, beginners, new)
}
else {
let delegate = PhysicsContactDelegate(for: self.base)
self.base.contactDelegate = delegate
physicsContatctDelegates[self.base] = (delegate, [:], [uuid: observer])
}
return Disposables.create {
physicsContatctDelegatesLock.lock(); defer { physicsContatctDelegatesLock.unlock() }
let (delegate, beginners, enders) = physicsContatctDelegates[self.base]!
var new = enders
new.removeValue(forKey: uuid)
if new.isEmpty && enders.isEmpty {
physicsContatctDelegates.removeValue(forKey: self.base)
}
else {
physicsContatctDelegates[self.base] = (delegate, beginners, new)
}
}
}
}
}
private
class PhysicsContactDelegate: NSObject, SKPhysicsContactDelegate {
init(for world: SKPhysicsWorld) {
self.world = world
super.init()
}
func didBegin(_ contact: SKPhysicsContact) {
physicsContatctDelegatesLock.lock(); defer { physicsContatctDelegatesLock.unlock() }
let (_, beginners, _) = physicsContatctDelegates[world]!
for each in beginners.values {
each.onNext(contact)
}
}
func didEnd(_ contact: SKPhysicsContact) {
physicsContatctDelegatesLock.lock(); defer { physicsContatctDelegatesLock.unlock() }
let (_, _, enders) = physicsContatctDelegates[world]!
for each in enders.values {
each.onNext(contact)
}
}
let world: SKPhysicsWorld
}
private let physicsContatctDelegatesLock = NSRecursiveLock()
private var physicsContatctDelegates: [SKPhysicsWorld: (SKPhysicsContactDelegate, [UUID: AnyObserver<SKPhysicsContact>], [UUID: AnyObserver<SKPhysicsContact>])] = [:]