-1

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.

Mutawe
  • 6,464
  • 3
  • 47
  • 90
lighter
  • 2,808
  • 3
  • 40
  • 59

2 Answers2

1

You have to add protocol above the interface you will be using.

@protocol MyViewControllerDelegate;

@interface MyViewController : UIViewController

@property (weak, nonatomic) id <MyViewControllerDelegate> delegate;
@property (copy, nonatomic) NSArray *viewControllers;
@end

@protocol MyViewControllerDelegate <NSObject>

@optional

//sth
@end
Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
0

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.

mttrb
  • 8,297
  • 3
  • 35
  • 57