Subclass an existing Swift Singleton - ios

I am adding functionality to an AWS class that creates a singleton (AWSIdentityManager) and the code for that class is in flux (AWS are improving it). I would like to make my added functionality more distinct from the AWS code so that I don't have to keep changing it when they upgrade.
Can I achieve this by subclassing or extension?
My goal is not to create another copy of the existing singleton, I just want to add methods (and hopefully properties) to it, without making too many changes in the released code.
I should note the following: The original class is written in Obj-C. I would like to have properties if possible.

To extend
extension AWSclass {
func functionA () { ...}
}
usage
AWSclass.shared.functionA()

Related

IOS Swift - Removing a inherited property from a class when implementing from SDK

I am attempting to use a Default Layout View Controller from a Software Developer Kit. This Default Layout contains elements of a generic UI which can be used for quick deployment and still allowing full functionality of the kits purpose. I am attempting to learn how to exclude certain members of a class I pull from this kit. I am more conceptually trying to understand how I would go about doing this.
Lets say this view controller class I am using has 4 class members that are properties (among other members that are methods, other properties)
Class: DefaultLayoutViewController
Properties:
topViewController
bottomViewController
leftViewController
rightViewController
I wish to remove (or hide, disable, etc.) the bottomViewController property, as its features have been replaced with other UI elements I created.
import SomeSDK
class MainViewController: DefaultLayoutViewController {
//Code for UI elements I created
}
The above code pulls all of the elements from the SDK's Class DefaultLayoutViewController, and builds my elements on top of it.
This property has this information about it in the documentation.
#property (nullable, nonatomic) SDKbottomViewController *bottomViewController
I have attempted to override from within the MainViewController class, but I am told 'Cannot override with a stored property'. How would I go about removing this inherited property from the class I am referencing? I obviously cannot simply delete the property from the initial class since I'm installing it in from an SDK Pod. I am newer to Swift, and coding in general, so perhaps I am just not wording my questions correctly in my research and failing to find an obvious solution that is documented out there. If this is repeated anywhere that I have missed, my apologies.
There is no way for a subclass to remove a property from a superclass. This violates the meaning of "inheritance." We usually refer to this as the Liskov Substitution Principle. There are various subtleties about how (and even if) this principle applies, but the way most programmers apply this is that anywhere DefaultLayoutViewController can be used, it is required that MainViewController also be usable. Consider this function:
func handleController(controller: DefaultLayoutViewController) {
doSomething(with: controller.bottomViewController)
}
If this were called with an instance of MainViewController, after you've "removed" bottomViewController, what should happen? This isn't a meaningful thing to do.
How you should handle this depends on exactly what you're trying to achieve. Generally the answer is "don't use class inheritance; use protocols." That may not be easy if you're tied to an existing class-based framework, but it doesn't change the fact that removing a property is not possible with inheritance.

Without exposing the interface in the public header of framework can I pass a custom object to the client application?

I am working with a objective-C framework.
I have a public framework header "MyPublicHeader.h" exposed to the client application. I have a custom class in the project,
//MyCustomClass.h file
#interface MyCustomClass.h
- (NSString *) methodA;
#end
//MyCustomClass.m file
#inplementation
- (NSString *) methodA {
}
#end
If I want the client to instantiate the class I have to make it as public framework header. I want to hide the interface as a curiosity, is there any way to do it???
First know that nothing can be truely hidden in Objective-C due to the nature of dynamic dispatch and the features in the runtime which allow discovery of methods etc.
That said there are a number of ways to do this, a couple:
Use a subclass. Declare a superclass and publish its interface as part of your framework. Make your class a subclass of this and publish its interface only within the framework. You define one or more init methods in the superclass which return and instance of the subclass, and if you want to expose any further API define it in the superclass with dummy (or faulting) implementations and less the subclass override etc. This approach is similar to the model used for classes like NSString.
A .h file is just text and you can exploit this: make two .h files, say MyCustomClass.h and InternalMyCustomClass.h. In the first just declare the interface with no members, or the API you wish to make public, and publish that to users of the framework. In the second declare the real interface used within the framework. You must make sure to keep all three of the files (2 .h, .m) in sync. This approach would be call little naughty by some, "here be dragons" by others, or "needs must" by yet others...
You might also like to look into "class extensions" which are related to categories.
Hope that satiates your curiosity a little, and keep up with the curiosity its good (except for cats)!
You could create an empty wrapper class which only holds a reference to your MyCustomClass object.
When they create this object you secretly instantiate an object of your MyCustomClass inside and extract it when they pass you an object of the wrapper class.
Not sure if this is exactly what you want to achieve, but could be a workaround.

Swift: UIViewController common function Inheritance

I came from Android background and I'm new to Swift.
I want to know how to use common functions in ViewControllers with DRY principles.
I need all of my ViewControllers to call following functions from one place:
isNetworkAvailable() //check app has internet return boolean
getLoggedinUser() //return logged in user object which I set before
showAlert(message:String) //display a popup
startLoader() // display a loader
stopLoader() // stop loader
As you can see these methods are global and I need to reuse them many times. So I don't want to copy paste the same code over and over again.
In Android I simply make one BaseActivity and put all those methods in it. Later I will extend the BaseActivity into SubActivities and inherit those common methods.
I'm not sure how to do this in "IOS Swift" as all the ViewControllers are attached to StoryBoard.
Can you please tell me what is the best way to achieve this? How does professional Swift developers handle situations like this?
(Please note - I don't want to delete story board and manually create UI elements.)
Thanks in Advance!
Create a CommonFunctions named public class (just a suggestion you can name it any class name which you want) extend it from NSObject and import UIKit Module in it because I think you also want to perform UI Operations in it like startLoader() & stopLoader() then you can write static global methods which you are mentioned in your question.
You can call then using CommonFunctions.methodName().
Or if you don't want to make static functions then create object of CommonFunctions class wherever you want to access those global methods and call the method which you want.

Access methods from other classes - is this a legitimate way

I'm pretty new to the whole coding scene and was trying to create a way of accessing similar methods from anywhere within my app. I created a class called HelperMethods : NSObject. I then defined all my helper methods like this: + (returnObject *) thisIsMyHelperMethodWithParameter:(object*)object; and then created the method body in the implementation. I then put an #import "HelperMethods.h" in my app prefix file so I can access it wherever I like in the app. I then call [HelperMethod thisIsMyHelperMethodWithParameter:]; from anywhere within the app. It works fine and I haven't had any problem, but is this a legitimate way of creating a central hub for useful methods?
Thanks,
Mike
Well better if you want to create the class which work as a central hub then create the singleton class and access anywhere fron the other class. The advantage of creating singleton class is that it shared single memory space. So that it will be less overhead, when accessed the methods and variable from other class.

How to create a class which is sub class of two classes

I have class called ViewController. How to make this class is a sub-class of "metaiosdkViewController" and "JWslideViewController". Help me with syntax.
i have written like this
#interface ViewController : MetaioSDKViewController,JWslideViewController
but this giving me error
objective-c doesn't support multiple inheritance,but if you want to add some extra behaviour you can achieve it through delegates..
yes objective-c doesnt support multiple inheritance but you can give one parent so
#interface ViewController : MetaioSDKViewController
and
#interface MetaioSDKViewController : JWslideViewController
this is just an idea I know you can implement well as per your need
What is it that you want to achieve with multiple inheritance?
Do you want to override methods from each of these super classes?
Note that objective c provides 2 mechanisms for extensibility:
1) Implementing a Protocol and make your object the delegate:
#interface ViewController : <MetaioSDKViewController,JWslideViewController>
This enforces ViewController to implement certain methods as defined in contract by 2 delegates, and at some point in processing, they get called. If you don't implement them, they may simply not be called but you may not get desired functionality.
Example: UITableViewDataSource protocol that defines many methods that UITableViewController subclass implements. cellForRowAtindexPath is very famous example of a delegate method that your own table view subclass must implement to draw your own custom cells.
Note that this is not the type of extensibility that subclasses provide in general sense. Your class does not extend any functionality here. Rather it becomes what it says - a delegate - someone who is assigned to do some task. Like you do:
yourTableView.delegate = self; //tell self to be the delegate of yourTableview
Library code does it's stuff and in some point in processing it calls [delegate someMethod]. If your own class implements it, it calls it, otherwise delegate will be nil, and it may just be NO-OP and you don't get desired functionality. Again, this is implementation-dependent. Maybe the protocol defines that the method is compulsory, in which case your class MUST implement this method in order to compile.
2) Implement a category:
This is sort of a shortcut way to extend library classes. They act like an extra stub which, when your code runs, attaches itself to the already existing memory layout of the library objects and provides extra functionality.
You can define a category on any of the in-built classes as well. In fact that is the primary objective it is used for. For example, here is an NSString category which provides HTML conversion. There are hundreds of categories implemented as open source and they provide enormous benefits where library code falls short. Discussing their suitability in entirety is however out of scope for this discussion.
One thing to note however is: You do not override anything using a category. Rather you are supplying something in extra. For example if you want some custom drawing across all your app views, you can define a category on UIView in your project and then all your views could simply include the category header file. You don't even have to inherit from this category, you simply inherit from the base type.
e.g. in the NSString category example above, you do not have to define your NSString to be of type NSString+HTML. Instead you just include the responsible NSString+HTML.h file wherever you want those extra methods like stringByConvertingHTMLToPlainText and so on. The changes remain limited to your project - to the files where you include this category.
Categories do not provide for extra data members - and that is something that only inheritance can provide. Yet, multiple inheritance among viewcontrollers is something you should definitely reconsider hundred times - you will see that what you are looking for is not multiple inheritance.

Resources