Objective-C interfaces, delegates, and protocols - ios

So I'm trying to wrap my head around Objective-C interfaces, delegates and protocols.
So I have a question:
Does a delegate have to be in a separate file or can it be methods defined in your class?
Is a protocol like a java interface? This is the way I understand it at the moment where it basically makes you implement methods if you use this protocol.
I'm still confused about interfaces. I'm pretty sure they have no resemblance to what an interface is in java. Maybe it's just a declaration of variables that will be implemented in the class.

A delegate protocol needs to be defined as such
#protocol
//methods
#end
it can be put in any .h class, you just need to import i t whenever you are going to use it.
A protocol is not like a java interface, a protocol is an adapter that allows two classes to works together. Basically it says, if you want class A to send you messages about its state and actions these are the methods it will call on its delegate that you must implement. Its not like an interface because an interface says if you want to subclass this class you must implement these methods, the protocol says if you want to interact with this class you must implement these methods, so its somewhat different.

The point of a delegate is to be notified when another object does something. For example one of your objects wants to know that a window is being closed, so you register it as the window's delegate and implement the windowWillClose: method. It will be called by NSWindow appropriately. So the delegate methods are usually defined in another class. Up to a certain point, it lets you extend the functionality of a class without subclassing it.
(Edit: see Daniel's answer about protocols.)
The #interface is the class declaration, where the member variables and methods are listed. It's located in the .h, that you import if you need to use the class. The code for methods sits in the #implementation in the .m file. In Java it's different, the .java file serves both purposes.

I would suggest checking out:
The Objective-C 2.0 Programming Language
It should have the answer to most of your questions about protocols and interfaces.
Protocols
Interfaces
As far as delegates, they can be a new object or the object in which you are creating the thing needing a delegate. Files don't really have anything to do with it.

Related

Two view controllers with similar functionality VIPER

I'm currently trying to implement VIPER-architecture in my project, and there is some questions I encountered with.
I have two modules in my app, that have some similar functionality (they both have imagePicker and ability to upload media to server, that implemented absolutely the same for both screens).
My question is how could I reuse this similar functionality in both modules? Trouble is that my imagePicker has many methods declared in Interactor that handle different events while selecting and uploading image (such as didUploadMediaFile(), didFailToUploadMediaFile(), uploadMediaFile() and so on).
Should I create third module with this functionality and then somehow subclass my other modules from it? Or maybe there is a better way of doing it?
The only similar components/methods I'd use are Data Managers, which can be shared between as many Interactors are you want, and yet being 100% compliant with VIPER architecture.
So, a DataManager called, for example, MediaApiDataManager() would be responsible for the implementation of the core code to UploadMediaFile() etc
I suggest you read this post for more great tips on VIPER: https://www.ckl.io/blog/best-practices-viper-architecture/
I think you need create abstract class and implement inside imagePicker logic. Declare interface (protocol) for it class with didUploadMediaFile(), didFailToUploadMediaFile(), uploadMediaFile() methods, implement this methods in class and inject to your VIPER modules
For the two modules try to abstract the similarities and try to build a Class of it. If both classes differs on the data type use Generics, also you can use Protocols, so declare the common methods of the two modules in one protocol and implement each one of them as an extension.
Maybe this tutorial helps. https://medium.com/#richiemon/protocol-extension-dispatching-6d5229f1338e

Best practices for ServiceCollection extension methods?

I'm trying to figure out how best to implement extension methods that register services when users of my library may want to override some of the default implementations of various interfaces. It seems like there are 2 ways of doing things.
The first way is to use services.TryAdd inside the extension method, which would enable you to register the override implementation by using services.Add before calling the main extension method so that your implementation gets there first. This works because .TryAdd won't add if there is already something registered for the interface.
If the extension method uses plain old services.Add instead of .TryAdd then you can still override the implementation but now you have to register your implementation after calling the main extension method that adds the default implementation.
To me it seems the first approach is the better one. If you call services.Add more than once with a different implementation of the interface then they both do get registered and you could actually take a dependency on an IEnumerable of the interface to get all of them. But if you really only need one implementation in the app then it seems better not to have any extra implementations in the collection. Obviously if the app does use multiple implementations of the interface then you would want to add all the ones you need.
I wonder if there is a consensus about this or a recommendation or is it just up to individual taste.

Automatically add code to custom subclasses

If I create a subclass of one of Apple's classes, ie: UIViewController, it comes with some methods already defined in the implementation file, ie: -viewDidLoad.
Is there a way to do this with my own classes?
I have been searching for the answer but haven't come up with anything.
Edit: To clarify my Question
I know how to subclass & use protocols etc.
What I want to do is have important methods that should be overridden by a subclass written into the implementation file automatically.
Better example:
If you subclass UITableViewController you will need to implement
-numberOfSections...
-numberOfRows...
-cellForRowAtIndex...
But you don't have to write them yourself, Apple automatically adds them into the file.
So is there something in obj-c that allows me to do that with my classes or is that something Apple has baked into xcode that can only be done by them?
These are basically boiler plate code which is written when the IDE is being designed itself.

Objective C: is Posing available for ios?

I am trying to implement posing for one ios project.
The scenario:
Defining class of controller at run time
I realise that poseAsClass or class_poseAs is not available for ios
& also deprecated for macOX.
will be grateful to any directions to implement posing in ios. Thanks
The whole pose / swizzle approach is really useful if you want to tamper with the OS / private SDK supplied classes - but you generally shouldn't be doing that and it's not a good idea to use it as a standard approach in your own code.
The scenario: Defining class of controller at run time
You would usually do this by using an abstract superclass / interface / #protocol to define the interface that your potential controllers need to implement and then switching them in and out at runtime.
In your case it seems that you would have one controller which acts as a proxy for the true controller. You also don't technically need an #protocol because UITableViewController is effectively your abstract superclass, but it would be best for your proxy to be a UITableViewController and own the view and for your other controllers to be NSObject subclasses and simply conform to the UITableView DataSource/Delegate protocols.
You should look into Method Swizzling. It helps you change the functions/function bodies at run time.
There is a great tutorial here.

Defining class of controller at run time [duplicate]

I am trying to implement posing for one ios project.
The scenario:
Defining class of controller at run time
I realise that poseAsClass or class_poseAs is not available for ios
& also deprecated for macOX.
will be grateful to any directions to implement posing in ios. Thanks
The whole pose / swizzle approach is really useful if you want to tamper with the OS / private SDK supplied classes - but you generally shouldn't be doing that and it's not a good idea to use it as a standard approach in your own code.
The scenario: Defining class of controller at run time
You would usually do this by using an abstract superclass / interface / #protocol to define the interface that your potential controllers need to implement and then switching them in and out at runtime.
In your case it seems that you would have one controller which acts as a proxy for the true controller. You also don't technically need an #protocol because UITableViewController is effectively your abstract superclass, but it would be best for your proxy to be a UITableViewController and own the view and for your other controllers to be NSObject subclasses and simply conform to the UITableView DataSource/Delegate protocols.
You should look into Method Swizzling. It helps you change the functions/function bodies at run time.
There is a great tutorial here.

Resources