Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing object pointer as protocol pointer

Tags:

objective-c

@protocol protoA <NSObject>
@end

@interface objA : NSObject<protoA> {
    @private
    }
@end

@implementation objA
@end

@protocol protoB <NSObject>
    -(void) foo: (id <protoA> *) par;
@end

@interface objB : NSObject<protoB> 
    -(void) foo: (id <protoA> *) par;
@end

@implementation objB
    -(void) foo: (id <protoA> *) par
    {
    //...
    }
@end

in some other class method i use it this way:

objB *obj1 = [[objB alloc] init];
objA *obj2 = [[objA alloc] init];

[obj1 foo: obj2];

I have compiler error: "Implicit conversion of an Objective-C pointer to '__autoreleasing id*' is disallowed with ARC

What is the proper way to do get this functionality ?

like image 633
gossamer Avatar asked Feb 07 '12 10:02

gossamer


3 Answers

id already is a pointer-type, use just id<Protocol> instead of id<Protocol>*.

like image 178
Georg Fritzsche Avatar answered Oct 20 '22 16:10

Georg Fritzsche


id is a pointer already, drop *:

-(void) foo: (id <protoA>) par
like image 45
hamstergene Avatar answered Oct 20 '22 15:10

hamstergene


If you want to specify type and protocol, you can use this form:

- (void)method:(MONType<MONProtocol>*)param;

If you want an opaque objc type (id) and protocol, use this form:

- (void)method:(id<MONProtocol>)param;
like image 25
justin Avatar answered Oct 20 '22 14:10

justin