我有一个 UITableView 的自定义子类,其中定义了一个协议(protocol),如下所示:
#import <UIKit/UIKit.h>
@protocol CustomDelegate <NSObject>
@optional
-(NSInteger)numberOfRows;
@end
@interface CustomTV : UITableView <UITableViewDelegate, UITableViewDataSource>{
id<CustomDelegate> *del;
}
@property (nonatomic, assign) id<CustomDelegate> *del;
@end
现在在其他一些类中,我实例化了这个 CustomTV 并将委托(delegate)设置为 self:
CustomTV *tbl = [[CustomTV alloc] initWithFrame:CGRectMake(0, 0, 200, 400) style:UITableViewStylePlain];
tbl.del = self;
为什么我在
tbl.del = self
行收到“不兼容的指针...”警告?我确实符合 header 中的 CustomDelegate 协议(protocol)。
最佳答案
您将委托(delegate)声明为指向对象指针的指针。类型id
已被声明为指向对象的指针,因此请移除星号。
@interface CustomTV : UITableView <UITableViewDelegate, UITableViewDataSource>{
id<CustomDelegate> del;
}
@property (nonatomic, assign) id<CustomDelegate> del;
@end
关于objective-c - "Incompatible pointer..."将委托(delegate)分配给自己时发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7638173/