I have a protocol
protocol AttibuteValueCellProtocol {
func set(attribute: String, value: String)
}
And in my tableView's datasource method I want my cell to confirm to this protocol. In objective-c I could do that, but in SWIFT it gives the error when I try to use as? UITableViewCell<AttibuteValueCellProtocol>
. In objective-C it always worked if I do ((UITableViewCell<AttibuteValueCellProtocol> *)cell
)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? UITableViewCell<AttibuteValueCellProtocol>
}
What can I replace it with?
P.S.
Error looks like this http://take.ms/OHRA3
I don't need extension , because when I deque cell , it will be CustomCell (and CustomCell conforms to Protocol), but this class can use different cell identifiers so this class shouldn't know about the cell class name and not every cell conforms to AttibuteValueCellProtocol , this class has to know that we have cell that conforms to protocol AttibuteValueCellProtocol
I Objective-C I can do that and it will compile (I understand that it will not work because view1 doesn't conform to protocol, but it's easy to create proper view and it will work,here I just give an example) :
#import "TestViewController.h"
@interface TestViewController ()
@end
@protocol TestProtocol <NSObject>
- (void)testMethod;
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIView *view1 = [[UIView alloc] init];
UIView *viewForProtocolJustToShow = (UIView<TestProtocol> *)view1;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…