Salman Ali
Salman Ali

Reputation: 255

Objective C Protocols

Hi i am new to objective c and i am trying to implement the following. There is a protocol that has a method. Then i create an object of that protocol in another class and in the third class i conform to protocol. Here is the code.

@protocol FirbaseUpdateDelegate <NSObject>

-(void)usersObtained: (NSArray *)users;

@end

Here is the first class,

#import "FirebaseDelegate.h"

@interface FirebaseConnection : NSObject
{
id delegate; // protocol object
}
-(void) loadUsers;

@end


@implementation UsersMessageListViewController

NSArray *allUsers;

- (void)viewDidLoad {
[super viewDidLoad];
FirebaseConnection *con = [[FirebaseConnection alloc] init];
//con.delegate = self; // i can not assign value to protocol here.
[con loadUsers];
// Do any additional setup after loading the view.
}
@end

This is not happening.

Upvotes: 0

Views: 214

Answers (1)

mag_zbc
mag_zbc

Reputation: 6982

Your delegate property doesn't conform to your protocol. Try

@interface FirebaseConnection : NSObject

@property (nonatomic, weak) id<FirbaseUpdateDelegate> delegate;

@end

In Objective-C id is a pointer to any Objective-C object. You must explicitly specify that it conforms to the protocol you want. Also, remember to set delegate properties as weak so as to avoid retain cycles.

Also, remember to make sure your class conforms to the protocol as well and implements its methods

@implementation UsersMessageListViewController () <FirbaseUpdateDelegate>

// stuff

-(void)usersObtained: (NSArray *)users 
{
    // do stuff
}

@end

Upvotes: 1

Related Questions