Self 类型的协议函数

Protocol function with Self type

代码如下:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@protocol HomeHeaderCellDelegate <NSObject>
- (void)didTapMoreLessMenuButton:(HomeHeaderCell *)cell;
@end

@interface HomeHeaderCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIView *secondMenuRowView;
@property (weak, nonatomic) IBOutlet UIButton *moreLessMenuButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *secondMenuRowViewHeightConstraint;
@property (weak, nonatomic) IBOutlet UIButton *askButton;
@property (weak, nonatomic) IBOutlet UIButton *contactButton;
@property (weak, nonatomic) IBOutlet UIButton *benchmarkButton;
@property (weak, nonatomic) IBOutlet UIButton *buySellButton;
@property (weak, nonatomic) IBOutlet UIButton *marketButton;

@property (nonatomic, weak) id <HomeHeaderCellDelegate> delegate;
@property BOOL isFullMenu;

- (void)toggleHeight;

@end

NS_ASSUME_NONNULL_END

这行有错误

- (void)didTapMoreLessMenuButton:(HomeHeaderCell *)cell;

它说:

Expected a type

你可以的

#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@protocol HomeHeaderCellDelegate;

@interface HomeHeaderCell : UITableViewCell
@property (nonatomic, weak) id <HomeHeaderCellDelegate> delegate;
@end

@protocol HomeHeaderCellDelegate <NSObject>
- (void)didTapMoreLessMenuButton:(HomeHeaderCell *)cell;
@end

NS_ASSUME_NONNULL_END

编译器需要知道 class HomeHeaderCell 在某处声明。

实际上,您必须使用 @import 语句 import class,但在这种情况下,您只需要类型而不需要实现细节。 @class 指令是前向引用,可确认类型但避免循环引用问题。

在导入行下添加@class,顺便使用现代的@import语句。

@import UIKit;
@class HomeHeaderCell;