简体   繁体   中英

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

I have a protocol in my open source library ( link in-case code samples aren't enough ).

Said protocol has two associated types, one for a function "send" type, and one for a "return type" defined like so:

/// 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 }
}

In a function implementation ( performFunction declared on another protocol):

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)

I create a CameraEvent object and attempt to downcast (unsure if that's the correct terminology) to T.ReturnType . At the point that is called, event is non-nil , however casting it to T.ReturnType gives a nil result even though it should match!

Event.get (.getEvent) is defined like so:

/// 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)
}

It's important to note as well that another implementation of the second protocol I talk about (The one defining the performFunction function) successfully does very similar logic and I don't get nil back! There must be something I'm doing wrong here, but I have no idea what it could be!

Screenshots in case that helps!

Calling the protocol's function

调用协议函数

Implementation of the protocol's function

协议功能的实现

This was... just the debugger being a pile of rubbish! Decided to add in a print statement to make absolutely sure it was nil . Turns out that wasn't the case! Prints fine, but lldb seems to think it's nil for whatever reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM