我正在尝试使其正常工作,但是我一定在某个地方犯了一个错误。根据C标头,这是Delegate类(已精简):

@protocol PaymentEngineMiddlewareDelegate <NSObject>

@required

/*!
    @brief Delegate method for startTransaction.
    @param transResponse - Contains all the transaction response info from the gateway
 */
-(void)transactionComplete :(PaymentEngineTransactionResponse *)transResponse;
@optional

/*!
    @brief Delegate method for device connected. The method gets called when the device is connected
 */
-(void)deviceConnected;

/*!
  @brief Delegate method for device disconnected. The method gets called when the device is disconnected
 */
-(void)deviceDisconnected;

@end


为此,我有:

// @interface PaymentEngineMiddlewareDelegate : NSObject
[BaseType(typeof(NSObject))]
[Model]
[Protocol]
interface PaymentEngineMiddlewareDelegate
{
    [Abstract]
    [Export("transactionComplete:")]
    void TransactionComplete(PaymentEngineTransactionResponse transResponse);

    [Export("deviceConnected")]
    void DeviceConnected();

    [Export("deviceDisconnected")]
    void DeviceDisconnected();
}


然后对于PaymentEngineMiddleware类,标头具有以下内容:

@interface PaymentEngineMiddleware : NSObject

+ (PaymentEngineMiddleware *) getInstance;

@property(nonatomic, strong) id<PaymentEngineMiddlewareDelegate> delegate;

//------------------------------Device Methods------------------------------

/*!
    @brief This method must be called before starting any transaction. Use it to connect to a device.
    @param deviceName The name of the device such as icmp or castle
    @param setDelegate Sets to self

 */
-(void)setDevice :(NSString *)deviceName :(id)setDelegate;

@end


为此,我有:

// @interface PaymentEngineMiddleware : NSObject
[BaseType(typeof(NSObject))]
[Protocol]
interface PaymentEngineMiddleware
{
    // +(id)getInstance;
    [Static]
    [Export("getInstance")]
    PaymentEngineMiddleware GetInstance { get; }

    [Export("delegate", ArgumentSemantic.Retain)]
    PaymentEngineMiddlewareDelegate Delegate { get; set; }

    [Export("setDevice:setDelegate:")]
    void SetDevice(string deviceName, PaymentEngineMiddlewareDelegate setDelegate);
}


在代码中,当我尝试调用“ middleware.SetDevice('name',this);”时如果这是一个从委托继承的类,则会得到异常。

有什么明显的我错过或弄错的地方吗?

最佳答案

您的setDevice方法选择器错误。

-(void)setDevice :(NSString *)deviceName :(id)setDelegate方法的正确选择器是setDevice::。这是因为此方法中的第二个参数没有外部名称。

绑定的最终条目应如下所示

[Export("setDevice::")]
void SetDevice(string deviceName, PaymentEngineMiddlewareDelegate setDelegate);


您可以在Apple docs中阅读有关如何构造Objective-C中的方法签名的更多信息。

关于ios - 在Xamarin中跟踪“无法识别的选择器发送到实例”异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46964439/

10-11 22:32