制作一个游戏,让生物可以和其他生物一起繁殖。我已经定义了一个协议,这样某些生物就可以繁殖。
然而,我似乎不知道如何正确地施放和通过该生物。
// Creature may breed with any other creature allowed to breed
@protocol Mateable
-(void)mateWith:(id<Mateable> *)entity;
@end当玩家接触一个生物时,我有逻辑来施放该生物并通过它:
// 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];
}但是,我得到了一个错误:
Cannot initialize the parameter of type '__autorelease <id>Mateable' with an lvalue of type '__strong id<Mateable>'
我如何才能正确地强制转换并传递对象作为参数?
发布于 2014-05-23 07:36:32
在id之后不需要*,因为id已经是指针类型
所以
// Creature may breed with any other creature allowed to breed
@protocol Mateable
-(void)mateWith:(id<Mateable>)entity;
@end 您应该能够自己找出它,因为您的mate是id<Mateable>,并且您将其传递给id<Mateable> *
https://stackoverflow.com/questions/23818760
复制相似问题