Automatically add code to custom subclasses - ios

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.

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

2 subclass on 1 UITextField

I got a question that i'm trying to figure out.
How can i set 2 different Subclasses in 1 UITextField?
I have installed a library ShowPassword(via cocoapod) and currently i'm using it so users can see the password that they are typing.
But i have also installed another library that makes an awesome UITextField (also via cocoapods)
https://github.com/raulriera/TextFieldEffects
And with this library i can insert Subclass Hoshi for example (which i have done in username text field)
How can i import both of them in password textfield?
You cannot!
What you would need to be able to to is create a new class which inherits from both the TextFieldEffects and the ShowPassword. But Swift (same as Objective-C) does not support multiple inheritance (see this answer).
Your only option is to create a custom subclass from one of the classes and create the effect of the second one on your own in your subclass! Meaning that you have to write code to implement the functionality of one of the two desired effects.
Alternatively you can try to create an extension for one of the two library classes adding the logic from the second one. (basically the same thing)
It is highly unlikely that both subclasses would work together without any problems. As soon as both class override the same methods you are in a trouble since you have to decide which one really overrides it, etc.

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.

Objective-C interfaces, delegates, and protocols

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.

Resources