如果我在Xcode中使用以下代码声明并且不完整地实现我自己的协议:
SomeProtocol.h:
@protocol SomeProtocol <NSObject>
@required
-(void)someRequiredMethod;
@end
Run Code Online (Sandbox Code Playgroud)
SomeImplementor.h:
#import "SomeProtocol.h"
@interface SomeImplementor : NSObject <SomeProtocol>
@end
Run Code Online (Sandbox Code Playgroud)
SomeImplementor.m:
#import "SomeImplementor.h"
@implementation SomeImplementor { // I get a warning on this line
}
@end
Run Code Online (Sandbox Code Playgroud)
然后Xcode在@implementationSomeImplementor.h 的行上抛出一个警告,内容如下:
实施不完整.
协议中的方法'someRequiredMethod'未实现.
但是,如果我UITableViewDataSource使用以下代码从UITableView.h 不完整地实现协议...
SomeClass.h:
@interface SomeClass : NSObject <UITableViewDataSource>
@end
Run Code Online (Sandbox Code Playgroud)
SomeClass.m:
#import "SomeClass.h"
@implementation SomeClass { // I think I should get a warning here, but I don't
}
@end
Run Code Online (Sandbox Code Playgroud)
...然后Xcode很好用,并且不会在任何地方显示警告,即使我显然没有从UITableViewDataSource协议实现以下方法:
@protocol UITableViewDataSource<NSObject>
@required
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// Row …Run Code Online (Sandbox Code Playgroud)