使用 Xamarin 为某些外部代码创建绑定库,我遇到了一个问题,尽管(据我所知)在协议中实现了所有选择器,但在 Obj-C 代码中,我的委托未能通过conformsToProtocol
检查结果,代码不起作用。
这是 Objective-C 头文件的样子(https://github.com/janrain/jump.ios/blob/master/Janrain/JRCapture/Classes/JRCapture.h):
@protocol JRCaptureDelegate <NSObject>
@optional
- (void)engageAuthenticationDialogDidFailToShowWithError:(NSError *)error;
- (void)engageAuthenticationDidCancel;
- (void)engageAuthenticationDidSucceedForUser:(NSDictionary *)engageAuthInfo forProvider:(NSString *)provider;
- (void)engageAuthenticationDidFailWithError:(NSError *)error forProvider:(NSString *)provider;
- (void)captureSignInDidSucceedForUser:(JRCaptureUser *)captureUser
status:(JRCaptureRecordStatus)captureRecordStatus;
- (void)captureSignInDidFailWithError:(NSError *)error;
- (void)registerUserDidSucceed:(JRCaptureUser *)registeredUser;
- (void)registerUserDidFailWithError:(NSError *)error;
- (void)refreshAccessTokenDidSucceedWithContext:(id <NSObject>)context;
- (void)refreshAccessTokenDidFailWithError:(NSError *)error context:(id <NSObject>)context;
@end
ApiDefinition.cs
这是与之对应的部分:
[Model, BaseType (typeof (NSObject))]
public partial interface JRCaptureDelegate {
[Export ("engageAuthenticationDidCancel")]
void EngageAuthenticationCancelled ();
[Export ("engageAuthenticationDidSucceedForUser:forProvider:")]
void AuthenticationSucceededForProvider (NSDictionary engageAuthInfo, string provider);
[Export ("captureSignInDidSucceedForUser:status:")]
void SignInSucceeded (JRCaptureUser captureUser, JRCaptureRecordStatus captureRecordStatus);
[Export ("registerUserDidSucceed:")]
void RegisterUserSucceeded(JRCaptureUser registeredUser);
[Export ("refreshAccessTokenDidSucceedWithContext:")]
void RefreshAccessTokenSucceeded(NSObject context);
//- (void)engageAuthenticationDialogDidFailToShowWithError:(NSError *)error
[Export ("engageAuthenticationDialogDidFailToShowWithError:")]
void DialogFailedToShow (NSError error);
//- (void)engageAuthenticationDidFailWithError:(NSError *)error forProvider:(NSString *)provider;
[Export("engageAuthenticationDidFailWithError:forProvider:")]
void AuthenticationFailedForProvider (NSError error, NSString provider);
//- (void)captureSignInDidFailWithError:(NSError *)error;
[Export("captureSignInDidFailWithError:")]
void SignInFailed (NSError error);
//- (void)registerUserDidFailWithError:(NSError *)error;
[Export("registerUserDidFailWithError:")]
void RegisterUserFailed (NSError error);
//- (void)refreshAccessTokenDidFailWithError:(NSError *)error context:(id <NSObject>)context;
[Export("refreshAccessTokenDidFailWithError:context:")]
void RefreshAccessTokenFailed (NSError error, NSObject context);
}
这可以编译并且看起来工作得很好,除了我遇到了一个似乎不想调用我的委托类中的方法的特定方法的问题。经过大量挖掘(并以艰难的方式学习Objective-C),我相信我已经隔离了这个问题。在我绑定的库中,我有这个:
if ([delegate conformsToProtocol:@protocol(JRCaptureDelegate)] &&
[delegate respondsToSelector:@selector(captureSignInDidSucceedForUser:status:)])
稍加检查DLog
,我发现我的代表未通过conformsToProtocol
检查(尽管它确实通过了respondsToSelector
检查)。
那么为什么我的课失败了conformsToProtocol
在实现协议中的所有方法时会失败。我在这里想念什么?
我的类实现了传递给采用 a 的各种方法的委托,JRCaptureDelegate
如下所示:
public class SignIn : JRCaptureDelegate
{
// overrides for each method
}