我想将我的委托(delegate)对象作为具有声明类型的参数传递,所以我不必强制转换(如果我通过 (id)sender 代替):
@protocol myObjectDelegate <NSObject>
- (void)myObjectAsArgument:(myObject *)object;
@end
@interface myObject : NSObject
// stuff...
@property (nonatomic, strong) id <myObjectDelegate> delegate;
@end
这样做的正确方法是什么?就像我说的,我知道我可以这样做:
- (void)myObjectAsArgument:(id)object;
那会让我通过
self
in 作为参数,但我不喜欢使用强制转换语法。谢谢。
笔记:
苹果也这样做:
@protocol UITableViewDataSource <NSObject>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
@end
@interface UITableView : UIScrollView
@property (nonatomic, strong) id <UITableViewDataSource> dataSource;
@end
这只是 UITableViewDataSource 协议(protocol)中的一种方法,它将 UITableView 类型作为参数传递。 ;)
最佳答案
由于这是顶级 Google 搜索结果,以下是操作方法:
@class myObject; // just need this for compiling
@protocol myObjectDelegate <NSObject>
- (void)myObjectAsArgument:(myObject *)object;
@end
@interface myObject : NSObject
// stuff...
@property (nonatomic, strong) id <myObjectDelegate> delegate;
@end
关于ios - 将委托(delegate)对象类型作为协议(protocol)方法参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25551714/