1

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.

Andreas ZUERCHER
  • 862
  • 1
  • 7
  • 20
  • 1
    The mechanism is called categories in objective-c https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html https://stackoverflow.com/questions/30859411/what-is-the-difference-between-protocol-extension-and-category-in-ios-developme https://stackoverflow.com/questions/5824755/can-a-category-implement-a-protocol-in-objective-c – Kamil.S Sep 06 '20 at 18:54

1 Answers1

1

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