无法在其构造函数中实例化具有id的对象

时间:2014-08-12 09:51:43

标签: objective-c ipad

我想实例化一个MposClient对象,它有这个接口:

#import <Foundation/Foundation.h>
#import "UICallback.h"
#import "LoginRequest.h"
#import "TransactionRequest.h"
#import "TYMessage.h"


@interface MPosClient : NSThread

/**
 * Default constructor.
 *
 * @param callback a third party application has to implement the UICallback interface and to<BR>
 *                 create it before to create the MPosClient (see UICallback documentation for<BR>
 *                 more information)
 */
- (id)initWithCallback:(id<UICallback>)callback;


- (void)loginWithRequest:(LoginRequest*)request;

@end

不幸的是,它的实现包含在.a文件中。 如您所见,构造函数方法采用id对象。 这就是我创建并实现以下界面的原因:

@interface PosMateCallback:NSObject<UICallback>
{
    NSString* currencyAphaCode;
    int currencyNumCode;
    double amount;
}

-(id) initCallback: (NSString*) currencyAlphaCode currencyNumCode:(int) currencyCode amount:(double) amountToPay;

@end

@implementation PosMateCallback
-(id) initCallback: (NSString*) currencyAlphaCode currencyNumCode:(int) currencyCode amount:(double) amountToPay{
    self = [super init];
    if (self)
    {
        currencyAphaCode = currencyAlphaCode;
        currencyNumCode = currencyCode;
        amount = amountToPay;
    }
    return self;
}

- (void)loginEndedWithResponse:(LoginResponse*)response{
    //NSLog(@" %@ ",[response getExtendedResultCode]);
    //TransactionType type = TransactionType.DEBIT;


}

- (NSString*)postUIRequest:(UIRequest*)request{
    //interruptedTransaction = request.getMessage().equals("ABANDON");
    return NULL;
}


- (void)transactionEndedWithResponse:(TransactionResponse*)response{

}


@end

所以,在我的viewController中,我尝试像这样实例化一个MPosClient对象:

id<UICallback> posmateCallback = [[PosMateCallback alloc] initCallback:@"EUR" currencyNumCode: 978 amount:42];

MPosClient *mposClient = [[MPosClient alloc] initWithCallback: *posmateCallback];

但是我得到以下编译错误:

Sending 'id<UICallback>' to parameter of incompatible type 'id<UICallback>'

我完全不明白为什么会这样,我出了什么问题?

2 个答案:

答案 0 :(得分:0)

看看这一行:

MPosClient *mposClient = [[MPosClient alloc] initWithCallback: *posmateCallback];

* posmateCallback应该是什么意思?它没有任何意义。您无法取消引用ID。

答案 1 :(得分:0)

将您的代码更改为:

#import <Foundation/Foundation.h>
#import "UICallback.h"
#import "LoginRequest.h"
#import "TransactionRequest.h"
#import "TYMessage.h"


@interface MPosClient : NSThread

/**
 * Default constructor.
 *
 * @param callback a third party application has to implement the UICallback interface and to<BR>
 *                 create it before to create the MPosClient (see UICallback documentation for<BR>
 *                 more information)
 */
- (id)initWithCallback:(UICallback *)callback;


- (void)loginWithRequest:(LoginRequest*)request;

@end

UICallback *posmateCallback = [[PosMateCallback alloc] initCallback:@"EUR" currencyNumCode: 978 amount:42];

MPosClient *mposClient = [[MPosClient alloc] initWithCallback:posmateCallback];