Objective c Can';t在构造函数中实例化id为的对象
我想实例化一个MposClient对象,该对象具有以下接口:Objective c Can';t在构造函数中实例化id为的对象,objective-c,ipad,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 c
#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
@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
#导入
#导入“UICallback.h”
#导入“LoginRequest.h”
#导入“TransactionRequest.h”
#导入“TYMessage.h”
@接口MPosClient:NSThread
/**
*默认构造函数。
*
*@param callback第三方应用程序必须实现UICallback接口并
*在创建MPosClient之前创建它(有关
*(更多信息)
*/
-(id)initWithCallback:(id)callback;
-(void)loginWithRequest:(LoginRequest*)请求;
@结束
不幸的是,它的实现包含在.a文件中。
正如您所看到的,构造函数方法接受一个id对象。
这就是我创建并实现以下接口的原因:
#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
@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
@接口PosMateCallback:NSObject
{
NSString*货币密码;
int currencyNumCode;
双倍金额;
}
-(id)initCallback:(NSString*)currencyAlphaCode currencyNumCode:(int)currencyCode金额:(double)金额支付;
@结束
@PosMateCallback的实现
-(id)initCallback:(NSString*)currencyAlphaCode currencyNumCode:(int)currencyCode金额:(double)金额支付{
self=[super init];
如果(自我)
{
currencyAphaCode=currencyAlphaCode;
currencyNumCode=currencyCode;
金额=支付金额;
}
回归自我;
}
-(void)LoginEndWithResponse:(LoginResponse*)响应{
//NSLog(@“%@,[response getExtendedResultCode]);
//TransactionType=TransactionType.DEBIT;
}
-(NSString*)假设请求:(UIRequest*)请求{
//interruptedTransaction=request.getMessage().equals(“放弃”);
返回NULL;
}
-(无效)transactionEndedWithResponse:(TransactionResponse*)响应{
}
@结束
因此,在我的viewController中,我尝试实例化一个MPosClient对象,如下所示:
id<UICallback> posmateCallback = [[PosMateCallback alloc] initCallback:@"EUR" currencyNumCode: 978 amount:42];
MPosClient *mposClient = [[MPosClient alloc] initWithCallback: *posmateCallback];
id posmateCallback=[[posmateCallback alloc]initCallback:@“欧元”货币代码:978金额:42];
MPosClient*MPosClient=[[MPosClient alloc]initWithCallback:*posmateCallback];
但我得到以下编译错误:
Sending 'id<UICallback>' to parameter of incompatible type 'id<UICallback>'
正在向不兼容类型“id”的参数发送“id”
我完全不明白为什么会发生这种情况,我出了什么问题?看这行:
MPosClient *mposClient = [[MPosClient alloc] initWithCallback: *posmateCallback];
这是什么意思?这没有任何意义。无法取消对id的引用。将代码更改为:
#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
为了让我相信这是实际的代码,请修复拼写错误,并养成以下划线字符开头实例变量名称的习惯。否则,您的代码会让您和其他人感到困惑。对不起,这是我的第一个ios应用程序。这有很多基本问题。首先,在传入UICallBack实例时,为什么要将其强制转换为id?将其更改为UICallBack*PostateCallback=。。。。然后您可以将参数类型更改为hyust UICallBack而不是id?当我删除它时,我有17个构建错误;是图书馆的错吗?我应该如何实例化它。铁律:任何错误都是你的错。2.要删除的是*posmateCallback开头的*。posmateCallback是一个id。您不能取消对id的引用。当我尝试更改MposClient时,会出现编译错误“应为类型”