Objective-c on the protocol defined a property protocol

lighter

I read some code, I founded the @protocol have defined a @property's protocol.

For example

protocol1.h

@protocol protocol2;
@protocol protocol1

-(void)p1_method1;
-(void)p1_method2;

@property (readonly, nonatomic) id<protocol2>p2;

@end

protocol2.h

@protocol protocol2

-(void)p2_method1;
-(void)p2_method2;

@end

I don't know the protocol have a @property protocol mean. Have a simple example? Thanks.

mttrb

All you need to do is @synthesize p2 in a class that conforms to protocol1. Properties from protocols don't get automatically synthesised.

@interface Class1 : NSObject <protocol1>

@end

@implementation Class1

@synthesize p2;      // Synthesize p2, the property from protocol1

- (void)p1_method1 {
    // Do something    
}

- (void)p1_method2 {
    // Do something else
}

This will create the correct getter/setter for the property (in your example the property is readonly so only a getter). The @synthesize will also create the ivar, in this case p2.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related