【问题标题】:Using Protocol with Objection to get instance of object使用带有异议的协议来获取对象的实例
【发布时间】:2013-10-14 09:32:55
【问题描述】:

我是目标 C 的新手,正在尝试实现反对(依赖注入器)。

但它在下面不起作用的是我正在使用的代码

My Protocol File 
#import <Foundation/Foundation.h>

@protocol InfoquestProtocolTest <NSObject>
-(void):nothing;
@end

My .h file is below
#import <Foundation/Foundation.h>
# import "InfoquestProtocolTest.h"


@interface InfoquestImplementation : NSObject<InfoquestProtocolTest>



@end

my .m file implementing protocol 


#import "InfoquestImplementation.h"

@implementation InfoquestImplementation
-(void):nothing{}
@end 

Code for module file of objection

#import "InfoquestTestConf.h"
#import <Objection/Objection.h>
#import "InfoquestViewController.h"
#import "InfoquestImplementation.h"
@implementation InfoquestTestConf
-(void)configure
{

    [self bindClass:[InfoquestImplementation class]   toProtocol:@protocol(InfoquestProtocolTest)];
}
@end

Code for getting object from objection

    JSObjectionInjector *injector = [JSObjection createInjector];
    [JSObjection setDefaultInjector:injector];
    InfoquestTestConf *Module = [[InfoquestTestConf alloc] init];
    [injector withModule: Module];
    id<InfoquestProtocolTest> testing2 = [injector getObject:[@protocol(InfoquestProtocolTest)];

但是当我尝试使用 [testing2 nothing] 调用时;我收到错误,自动完成没有显示任何内容。

谢谢 高拉夫

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    你有一个语法错误:

    你应该替换:

    -(void):nothing;
    

    -(void)nothing;
    

    【讨论】:

      【解决方案2】:

      这是您的语法错误。请更改:

          -(void):nothing;
      

      到:

          -(void)nothing;
      

      【讨论】:

        【解决方案3】:

        因此您使用的是自定义委托。因此,首先您必须将委托设置为您正在实现协议方法的类。

        【讨论】:

        • 请多解释一下,我在自定义委托中没有发言权,我知道委托,因为它们是在 tableviewcontroller 中实现的。
        • 自定义委托只是您在协议中使用您的定义方法编写自己的委托。而且,当您在协议中定义方法时,您必须在另一个要使用的类中实现相同的方法
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多