开发者

When using Objection, implementation of custom protocol crashes with unrecognized selector

开发者 https://www.devze.com 2023-03-23 16:59 出处:网络
I am defining a custom protocol: @protocol NGSAuthProvider <NSObject> - (BOOL)isReady; - (BOOL)isSessionValid;

I am defining a custom protocol:

@protocol NGSAuthProvider <NSObject>
- (BOOL)isReady;
- (BOOL)isSessionValid;
- (void)login;
- (void)logout;
- (NSString *)accessToken;
- (BOOL)handleOpenURL:(NSURL *)url;
@end

I want to have different providers. So one is a Facebook provider:

@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

I setup Objection to bind from my class to the protocol.

@interface NGSObjectionModule : ObjectionModule
@end

@implementation NGSObjectionModule

- (void)configure 
{
   self bind:[NGSFacebookAuthProvider class] toProtocol:@protocol(NGSAuthProvider)];
}
@end

I setup the Global Injector:

@implementation NGSAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  ObjectionModule *module = [[NGSObjectionModule alloc] init];
  ObjectionInjector *injector = [Objection createInjector:module];
  [module release];

  [Objection setGlobalInjector:injector];
}

I am using this in my RootViewController like this:

@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

When I run this, I get the following error:

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

So my class implements the protocol. It successfully is assigned to id<NGSAuthProvider>. I tried contructing [[NGSFacebookAuthProvider alloc] init] explicitly instead of using Object开发者_StackOverflowion and it still crashed.

I tried looping through the selectors using objc/runtime.h methods to see which selectors are there but the only thing it finds is 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);
}

This has to be something simple that I am missing. I use protocols defined by Cocoa and don't have this issue. I have defined custom protocols for UIViewController-based delegates without issue.

I am stumped as to why Obj-C runtime can't find my methods! If I change id<NGSAuthProvider> to NGSFacebookAuthProvider and construct it explicitly then it all works.

SOLUTION:

The problem was I misunderstood how to bind to a protocol. One way that works is:

@implementation NGSObjectionModule

- (void)configure 
{
  [self bind:[[[NGSFacebookAuthProvider alloc] init] autorelease] toProtocol:@protocol(NGSAuthProvider)];
}
@end

What I would like to do is bind a class to a protocol, but Objection probably wouldn't know the initializer to call?


The issue is you're trying to use the static class method (denoted because you've got a +) instead of a method run on an instance of your object (which is what you've written it as, with a -)


Chris,

You could use Objection's meta class bindings that allows you to bind a meta class to a protocol and invoke class methods against the meta class instance.

For example,

[self bindMetaClass:[NGSFacebookAuthProvider class] toProtocol:@protocol(NGSAuthProvider)];

But only if you want to use class methods. Otherwise you can use the protocol bindings against a share instance.


I also faced the same issue as I was using

[self bind:[MyClass class] toProtocol:@protocol(MyProtocol)];

The right way for it to work is

[self bindClass:[MyClass class] toProtocol:@protocol(MyProtocol)];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号