Objective c 如何使接口实例符合id<;类型的协议&燃气轮机;
我正在学习objective-c,我正在努力学习@protocol。我开发了下面的示例。但我不清楚的是 main_1和main_2中的代码。对于main_1,我认为它可能或多或少类似于java,例如,*sampleClass是sampleClass的实例,因此,它不应该访问ProcessComplete方法。然而,在Objective-c中,代码是有效的 对于main_2,我试图实例化包含SampleClass的SampleClass对象作为协议的实现者。与main_1中的代码不同,我假设或认为main_1中的代码是SampleClass的提供实例,但不是协议的实现者 如果我不对,请纠正我。 2-请让我知道为什么在代码main_2中,我收到以下错误:Objective c 如何使接口实例符合id<;类型的协议&燃气轮机;,objective-c,delegates,protocols,Objective C,Delegates,Protocols,我正在学习objective-c,我正在努力学习@protocol。我开发了下面的示例。但我不清楚的是 main_1和main_2中的代码。对于main_1,我认为它可能或多或少类似于java,例如,*sampleClass是sampleClass的实例,因此,它不应该访问ProcessComplete方法。然而,在Objective-c中,代码是有效的 对于main_2,我试图实例化包含SampleClass的SampleClass对象作为协议的实现者。与main_1中的代码不同,我假设或认为
Implicit conversion of an Objective-C pointer to '__strong id<PrintClientProtocol> *' is disallowed with ARC
Pointer to non-const type 'id<PrintClientProtocol>' with no explicit ownership
Incompatible pointer types initializing '__strong id<PrintClientProtocol> *' with an expression of type 'SampleClass *'
main_2
id<PrintClientProtocol> *sampleClass2 = [[SampleClass alloc] init];
[sampleClass2 startAction];
[sampleClass2 processCompleted];
printClass.m
#import <Foundation/Foundation.h>
#import "PrintClass.h"
#import "SampleClass.h"
@implementation PrintClass
- (void) printDetails {
NSLog(@"Printing Details");
[implementor processCompleted];
}
- (void) setDelegate:(id)newDelegate {
implementor = newDelegate;
}
@end
#import <Foundation/Foundation.h>
#import "SampleClass.h"
#import "PrintClass.h"
@implementation SampleClass
-(void)startAction {
PrintClass *printClass = [[PrintClass alloc] init];
[printClass setDelegate: self];
[printClass printDetails];
}
-(void) processCompleted {
NSLog(@"PROCESS_COMPLETED.....");
}
@end
#导入
#导入“PrintClass.h”
#导入“SampleClass.h”
@实现PrintClass
-(作废)打印详细信息{
NSLog(“打印详细信息”);
[实现者进程完成];
}
-(void)setDelegate:(id)newDelegate{
implementor=newDelegate;
}
@结束
SampleClass.h
#ifndef PrintClass_h
#define PrintClass_h
#endif /* PrintClass_h */
//The class/interface that to execute the protocols in the client contract. execution is to be done according
//to the DELEGATE Class/Interface
@interface PrintClass :NSObject {
id implementor;//instance of the DELEGATE/implementor and executor class/interface
//change id to SampleClass for testing
}
- (void) printDetails;
- (void) setDelegate: (id) implementor;
@end
#ifndef SampleClass_h
#define SampleClass_h
#endif /* SampleClass_h */
#import "PrintClientProtocol.h"
//the class/interface that provide implementions and execution of it, which is the "DELEGATE"
@interface SampleClass : NSObject<PrintClientProtocol>
- (void)startAction;
@end
\ifndef样本类
#定义样本类
#endif/*样本类*/
#导入“PrintClientProtocol.h”
//提供实现和执行的类/接口,即“委托”
@接口示例类:NSObject
-(无效)startAction;
@结束
SampleClass.m
#import <Foundation/Foundation.h>
#import "PrintClass.h"
#import "SampleClass.h"
@implementation PrintClass
- (void) printDetails {
NSLog(@"Printing Details");
[implementor processCompleted];
}
- (void) setDelegate:(id)newDelegate {
implementor = newDelegate;
}
@end
#import <Foundation/Foundation.h>
#import "SampleClass.h"
#import "PrintClass.h"
@implementation SampleClass
-(void)startAction {
PrintClass *printClass = [[PrintClass alloc] init];
[printClass setDelegate: self];
[printClass printDetails];
}
-(void) processCompleted {
NSLog(@"PROCESS_COMPLETED.....");
}
@end
#导入
#导入“SampleClass.h”
#导入“PrintClass.h”
@实现示例类
-(无效)startAction{
PrintClass*PrintClass=[[PrintClass alloc]init];
[printClass setDelegate:self];
[printClass printDetails];
}
-(作废)处理完成{
NSLog(@“流程完成…”);
}
@结束
PrintClientProtocol:
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
//the protocol that demands or needs implementor and executor "Client/Delegatee Protocol"
@protocol PrintClientProtocol <NSObject>
- (void) processCompleted;
@end
NS_ASSUME_NONNULL_END
#导入
NS\u假设\u非空\u开始
//要求或需要实施者和执行者的协议“客户机/被授权者协议”
@协议PrintClientProtocol
-(无效)已完成;
@结束
NS\u假设\u非空\u结束
类的声明SampleClass
表示它符合协议PrintClientProtocol
。这意味着它会自动继承协议PrintClientProtocol
中声明的所有成员,包括-processCompleted
。这与您在Java中所期望的相同
在main_2
中,问题在于类型id
已经是指向对象类型的指针,类似于SampleClass*
。通过编写id*
,您正在编写指向对象类型指针的指针,该指针与右侧不兼容,右侧有指向对象类型的指针