我正在定义一个自定义协议:
@protocol NGSAuthProvider <NSObject>
- (BOOL)isReady;
- (BOOL)isSessionValid;
- (void)login;
- (void)logout;
- (NSString *)accessToken;
- (BOOL)handleOpenURL:(NSURL *)url;
@end
我想有不同的供应商。所以一个是 Facebook 提供商:
@interface NGSFacebookAuthProvider : NSObject <NGSAuthProvider>
@end
@interface NGSFacebookAuthProvider () <FBSessionDelegate>
@property BOOL ready;
@property(nonatomic, retain) Facebook *facebook;
@property(nonatomic, retain) NSArray *permissions;
@end
@implementation NGSFacebookAuthProvider
//Implementation of fbLogin, fbLogout and the methods in NGSAuthProvider that forward calls to self.facebook
- (NSString *)accessToken
{
return [self.facebook accessToken];
}
@end
我设置了反对从我的班级绑定到协议。
@interface NGSObjectionModule : ObjectionModule
@end
@implementation NGSObjectionModule
- (void)configure
{
self bind:[NGSFacebookAuthProvider class] toProtocol:@protocol(NGSAuthProvider)];
}
@end
我设置了全局注入器:
@implementation NGSAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ObjectionModule *module = [[NGSObjectionModule alloc] init];
ObjectionInjector *injector = [Objection createInjector:module];
[module release];
[Objection setGlobalInjector:injector];
}
我在我的 RootViewController 中使用它,如下所示:
@interface RootViewController : UITableViewController
@end
@interface RootViewController ()
@property(nonatomic, retain) id<NGSAuthProvider> authProvider;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.authProvider = [[Objection globalInjector] getObject:@protocol(NGSAuthProvider)];
}
- (void)processConfig {
NSString *token = [self.authProvider accessToken];
// use the access token
}
@end
当我运行它时,我收到以下错误:
2011-07-26 21:46:10.544 ngs[6133:b603] +[NGSFacebookAuthProvider accessToken]: unrecognized selector sent to class 0x30c7c
2011-07-26 21:46:10.546 ngs[6133:b603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NGSFacebookAuthProvider accessToken]: unrecognized selector sent to class 0x30c7c'
*** Call stack at first throw:
(
0 CoreFoundation 0x00e825a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00fd6313 objc_exception_throw + 44
2 CoreFoundation 0x00e8417b +[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00df3966 ___forwarding___ + 966
4 CoreFoundation 0x00df3522 _CF_forwarding_prep_0 + 50
5 ngs 0x0000324b -[RootViewController processConfig] + 731
6 ngs 0x000041a2 __33-[RootViewController viewDidLoad]_block_invoke_0 + 50
所以我的班级实现了协议。它已成功分配给id<NGSAuthProvider>
。我尝试[[NGSFacebookAuthProvider alloc] init]
显式构建而不是使用反对,但它仍然崩溃。
我尝试使用方法循环选择器objc/runtime.h
以查看存在哪些选择器,但它发现的唯一内容是initialize
:
- (void)logSelectors:(id)obj
{
int i=0;
unsigned int mc = 0;
Method * mlist = class_copyMethodList(object_getClass([obj class]), &mc);
NSLog(@"%d methods", mc);
for(i=0;i<mc;i++)
NSLog(@"Method no #%d: %s", i, sel_getName(method_getName(mlist[i])));
free(mlist);
}
这必须是我想念的简单的东西。我使用 Cocoa 定义的协议,没有这个问题。UIViewController
我已经为-based 代表定义了自定义协议,没有问题。
我很困惑为什么 Obj-C 运行时找不到我的方法!如果我更改id<NGSAuthProvider>
为NGSFacebookAuthProvider
并明确构建它,那么一切正常。
解决方案:
问题是我误解了如何绑定到协议。一种有效的方法是:
@implementation NGSObjectionModule
- (void)configure
{
[self bind:[[[NGSFacebookAuthProvider alloc] init] autorelease] toProtocol:@protocol(NGSAuthProvider)];
}
@end
我想做的是将一个类绑定到一个协议,但 Objection 可能不知道要调用的初始化程序?