@protocol CETableViewModelProtocol <NSObject>
@property (nonatomic, strong) Class cellClass;
@property (nonatomic, copy ) NSString* cellType;
@end
協(xié)議中是可以定義屬性的,但是只有對應的getter和setter方法 但是沒有對應的成員變量 而getter和setter就是操作的對應成員變量 所以就無法調用getter或者setter 否則項目崩潰
想要調用的話 需要在實現這個協(xié)議的類中用@synthesize cellType = _cellType;來聲明成員變量,這樣這個類的對象就擁有了協(xié)議中定義的這些屬性,用法跟自身其他屬性一樣就不啰嗦了。
來看看實例吧:
.h
#import <Foundation/Foundation.h>
#import "CETableViewModelProtocol.h"
NS_ASSUME_NONNULL_BEGIN
@interface CETableViewModel : NSObject <CETableViewModelProtocol>
@end
NS_ASSUME_NONNULL_END
.m
#import "CETableViewModel.h"
@implementation CETableViewModel
@synthesize cellHeight = _cellHeight;
@synthesize cellType = _cellType;
@end
個人博客地址:https://youyou0909.github.io