我刚刚通过快速测试尝试了一些东西,我有一个问题,在下面的代码中:
@protocol stuffieProtocol <NSObject>
@required
-(void)favouiteBiscuit;
@end
.
// DOG & TED ARE IDENTICAL, THEY JUST LIKE DIFFERENT BISCUITS
@interface Dog : NSObject <stuffieProtocol>
@property (strong, nonatomic) NSString *name;
@end
@implementation Dog
- (id)init {
return [self initWithName:@"Unknown"];
}
- (id)initWithName:(NSString *)name {
self = [super init];
if(self) {
_name = name;
}
return self;
}
- (void)whoAreYou {
NSLog(@"MY NAME IS: %@ I AM A: %@", [self name], [self class]);
}
- (void)favouiteBiscuit {
NSLog(@"FAVOURITE BISCUIT IS: Costa Jam Biscuit");
}
@end
.
Dog *stuffie_001 = [[Dog alloc] initWithName:@"Dog Armstrong"];
Ted *stuffie_002 = [[Ted alloc] initWithName:@"Teddy Sullivan"];
NSArray *stuffieArray = @[stuffie_001, stuffie_002];
for(id<stuffieProtocol> eachObject in stuffieArray) {
[eachObject whoAreYou]; // << ERROR
[eachObject favouiteBiscuit];
}
我的问题是我收到一个错误"ARC Semantic Issue: No known instance method for selector 'whoAreYou'"
[eachObject whoAreYou];
如果我加上前缀[(Dog *)eachObject whoAreYou];
then 这适用于循环的所有迭代,但感觉不对,因为数组中的所有对象都不是 type Dog
。
我应该用什么前缀才能正确?