【发布时间】:2010-02-15 09:34:47
【问题描述】:
当我试图在 protocolB 的对象上调用 protocolA 方法时,我会收到编译器警告”,但是我如何将它限制为仅 protocolB 的方法,我的意思是它不应该允许我运行代码,它必须给我一个错误???在Objective-C中有什么方法可以解决这个问题?
例子:
// this is protocolA.h
@protocol protocolA
-(void)methodA;
@end
//this is protocolB.h
#import "protocolA"
@protocol protocolB <protocolA>
-(void)methodB;
@end
//this is MyClass.h
#import "protocolA"
@interface MyClass <protocolA>
{}
@end
//this is MyClass.m
@implementation
-(void)applicationDidFinishLaunching
{
id<protocolA> objA = [[MyClass alloc]init];
[objA methodA];//Should work fine
[objA methodB];//**Sholud give me error, but only warning is prompted but able to access methodB using objA and print its contents**
}
-(void)methodA
{
NSLog(@"This is in protocolA");
}
-(void)methodB
{
NSLog(@"This is in protocolB");
}
@end
注意:这里我在protocolB中扩展protocolA,但是当我在MyClass中创建protocolA的对象时,我应该只能访问protocolA的methodA而不是protocolB的methodB。
【问题讨论】:
标签: objective-c protocols