Objc - Extension - TableView Delegate

259 Views Asked by At

I am trying to follow the Viper pattern in a small objc project. I get the different roles of each part with no particular issue. However, the part I have an issue with is when I try to move the delegate/datasource of my tableview to another file, because I read that this is how it's supposed to be done. I followed that post: iOS using VIPER with UITableView but I can't manage to compile.

This issue here is that I have no idea on how to make an extension in Objc. I tried a lot of syntaxes, but none of them worked. How (by example) would I properly have in VIPER "MyViewController.m/h" & "MyTableViewController.m/h" where "MyTableViewController" is an extension of "MyViewController"? Meaning that we would see <UITableViewDelegate> in "MyViewController.h".

Thanks a lot for your help. This is probably a redundant question but I didn't manage to find a clear answer, if any, to my extension issue.

1

There are 1 best solutions below

1
On BEST ANSWER

Thanks to @Kamil.S in the comment above, I manage to find what I wanted in the apple documentation! Indeed, extensions in Objc are called "Categories". I pretty much did what was written on the post I linked in my original question.

So here's a simplified example if anyone needs it:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) id<ViewToPresenterProtocol> presenter;
@end

ViewController.m

#import "ViewController.h"

@implementation ViewController
// All my code, ViewDidLoad, and so on
@end

CollectionViewController.h

#import <UIKit/UIKit.h>
#import "ViewController.h"

@interface ViewController (CollectionViewController) <UICollectionViewDelegate, UICollectionViewDataSource>
@end

CollectionViewController.m

#import <UIKit/UIKit.h>
#import "CollectionViewController.h"
#import "ViewController.h"

@implementation ViewController (CollectionViewController)

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.presenter getNumberOfItems];
}

// ...
// Here add others functions for CollectionView Delegate/Datasource protocols

@end