简体   繁体   English

如何创建接口的实例符合类型 id<..> 的协议

[英]How to cretae instance of Interface conform to protocol as of type id<..>

I am leaning objective-c, and I am trying to learn the @protocol.我正在学习 objective-c,我正在尝试学习 @protocol。 I developed the below example.我开发了以下示例。 But what is not clear to me is The code in main_1 and main_2.但我不清楚的是 main_1 和 main_2 中的代码。 For main_1, I thought it might be more or less like java for example, *sampleClass is instance of SampleClass, hence, it should not have access to ProcessComplete method.对于 main_1,我认为它可能或多或少类似于 java,例如,*sampleClass 是 SampleClass 的实例,因此,它不应该访问 ProcessComplete 方法。 However, in Objective-c, the code works….但是,在 Objective-c 中,代码有效……。

For main_2, I am trying to instantiate object of SampleClass that contains SampleClass as implementor of the protocol.对于 main_2,我正在尝试实例化包含 SampleClass 作为协议实现者的 SampleClass 的 object。 Unlike the code in main_1, I supposed or thought that in the code in main_1 is provide instance of SampleClass but NOT as implementor of the protocol.与 main_1 中的代码不同,我假设或认为 main_1 中的代码是提供 SampleClass 的实例,但不是作为协议的实现者。

1-Please correct me if I am not right. 1-如果我不正确,请纠正我。 2-please let me know why in code main_2, I received the following errors: 2-请告诉我为什么在代码 main_2 中,我收到以下错误:

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_1 main_1

    SampleClass *sampleClass = [[SampleClass alloc] init];
    [sampleClass startAction];
    [sampleClass processCompleted];

main_2 main_2

    id<PrintClientProtocol> *sampleClass2 = [[SampleClass alloc] init];
    [sampleClass2 startAction];
    [sampleClass2 processCompleted];

printClass.h打印类.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

printClass.m打印类.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

SampleClass.h样本类.h

#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

SampleClass.m样本类.m

#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

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

The declaration of the class SampleClass says that it conforms to the protocol PrintClientProtocol . class SampleClass的声明说它符合协议PrintClientProtocol That means it automatically inherits all the members declared in the protocol PrintClientProtocol , including -processCompleted .这意味着它会自动继承协议PrintClientProtocol中声明的所有成员,包括-processCompleted This is the same as what you would expect in Java.这与您在 Java 中所期望的相同。

In main_2 , the problem is that the type id<PrintClientProtocol> is already a pointer-to-object type, akin to SampleClass * .main_2中,问题在于类型id<PrintClientProtocol>已经是指向对象的类型,类似于SampleClass * By writing id<PrintClientProtocol> * , you are writing a pointer-to-pointer-to-object type, which is not compatible with the right side, which has a pointer-to-object type.通过编写id<PrintClientProtocol> * ,您正在编写一个指向对象的指针类型,它与具有指向对象类型的右侧不兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM