转换和传递协议作为方法参数

hel*_*on3 1 objective-c ios

制作一个生物可以与他人一起复制的游戏.我已经定义了一个协议,以便某些生物可以繁殖.

但是,我似乎无法弄清楚如何正确施放和传递生物

// Creature may breed with any other creature allowed to breed
@protocol Mateable
-(void)mateWith:(id<Mateable> *)entity;
@end
Run Code Online (Sandbox Code Playgroud)

当玩家触摸一个生物时,我有逻辑施放该生物并传递它:

// If user taps on a create that can breed, we make it happen...
if( [touching conformsToProtocol:@protocol(Mateable)] ){
    id<Mateable> mate = (id<Mateable>) touching;
    [player mateWith:mate];
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到一个错误:

Cannot initialize the parameter of type '__autorelease <id>Mateable' with an lvalue of type '__strong id<Mateable>'

我怎样才能正确地施放并传递生物作为参数?

Bry*_*hen 5

你不需要*之后id因为id已经是指针类型

所以

// Creature may breed with any other creature allowed to breed
@protocol Mateable
-(void)mateWith:(id<Mateable>)entity;
@end 
Run Code Online (Sandbox Code Playgroud)

你应该能够自己解决这个问题,因为你mateid<Mateable>并且你将它传递给了id<Mateable> *