可选转换为协议的关联类型失败(返回 nil)

Optional cast to protocol's associated type is failing (returning nil)

我的开源库中有一个协议 (link in-case code samples aren't enough)。

所述协议有两种关联类型,一种用于函数 "send" 类型,另一种用于 "return type" 定义如下:

/// A protocol which can be adopted to define a function that can be performed on any given camera
public protocol CameraFunction {

    /// An associated type which is taken as an input when calling this function on a `Camera` instance
    associatedtype SendType

    /// An associated type which is returned from the camera when calling this function on the `Camera`
    associatedtype ReturnType

    /// The enum representation of this given function
    var function: _CameraFunction { get }
}

在函数实现中(performFunction 在另一个协议上声明):

func performFunction<T>(_ function: T, payload: T.SendType?, callback: @escaping ((Error?, T.ReturnType?) -> Void)) where T : CameraFunction {

        switch function.function {
        case .getEvent:
            let packet = Packet.commandRequestPacket(code: .getAllDevicePropData, arguments: [0], transactionId: ptpIPClient?.getNextTransactionId() ?? 0)
            ptpIPClient?.awaitDataFor(transactionId: packet.transactionId, callback: { (dataResult) in

                switch dataResult {
                case .success(let data):
                    guard let numberOfProperties = data.data[qWord: 0] else { return }
                    var offset: UInt = UInt(MemoryLayout<QWord>.size)
                    var properties: [PTPDeviceProperty] = []
                    for _ in 0..<numberOfProperties {
                        guard let property = data.data.getDeviceProperty(at: offset) else { break }
                        properties.append(property)
                        offset += property.length
                    }
                    let event = CameraEvent(sonyDeviceProperties: properties)
                    callback(nil, event as? T.ReturnType)
                case .failure(let error):
                    callback(error, nil)
                }
            })
            ptpIPClient?.sendCommandRequestPacket(packet, callback: nil)

我创建了一个 CameraEvent 对象并尝试向下转型(不确定这是否是正确的术语)到 T.ReturnType。在调用点,eventnon-nil,但是将其转换为 T.ReturnType 会给出 nil 结果,即使它应该匹配!

Event.get (.getEvent) 定义如下:

/// Functions for interacting with the event API on the camera
public struct Event: CameraFunction {

    public var function: _CameraFunction

    public typealias SendType = Bool

    public typealias ReturnType = CameraEvent

    /// Gets the current event
    public static let get = Event(function: .getEvent)
}

同样重要的是要注意,我谈论的第二个协议的另一个实现(定义 performFunction 函数的那个​​)成功地执行了非常相似的逻辑,我没有得到 nil !一定是我做错了什么,但我不知道它可能是什么!

截图以防有帮助!

调用协议的函数

协议功能的实现

这就是...调试器是一堆垃圾!决定添加 print 语句以绝对确保它是 nil。事实证明并非如此!打印正常,但 lldb 似乎出于某种原因认为它为零。