1

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.

Bruce0
  • 2,718
  • 1
  • 17
  • 21
  • [This question](http://stackoverflow.com/questions/37530346/what-is-protocol-oriented-programming-in-swift-what-added-value-does-it-bring) my also be of help – mfaani Nov 13 '16 at 20:59

1 Answers1

2

To extend

extension AWSclass {
func functionA () { ...}

}

usage

AWSclass.shared.functionA()
mfaani
  • 33,269
  • 19
  • 164
  • 293
  • ok - so you are saying do it as an extension. But in that case I think I cannot add properties correct? Also I should have mentioned that the AWS class is in Obj-c, should I be using categories or is a swift extension as good as I can do. – Bruce0 Nov 13 '16 at 21:00
  • @Bruce0 In Swift you *can't* have [stored properties](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html#//apple_ref/doc/uid/TP40014097-CH14-ID255). However you *can* have [computed properties](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html#//apple_ref/doc/uid/TP40014097-CH14-ID259). In case of it being in Objective-C you have to use [bridging](http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift) – mfaani Nov 13 '16 at 21:06
  • 1
    You can also add [type properties](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html#//apple_ref/doc/uid/TP40014097-CH14-ID264) as long as you provide an initializer expression or a getter/setter. –  Nov 13 '16 at 21:14
  • @Bruce0 For my previous comment I meant you can't have a stored property in an extension. Normal classes can have stored properties... – mfaani Nov 13 '16 at 21:16
  • @Honey is there anyway I can override one of the existing methods in the singleton? ( I know Extensions don't allow it). I want to get control when the initialization takes place if possible, but then pass it back to the original class func after some processing. Maybe I should do an extension for the extended functionality, and then create a separate class that manages added initialization functionality. It just seems clumsy – Bruce0 Nov 13 '16 at 21:18
  • @Bruce0 I think just as you said you can't [**override** in extensions](http://stackoverflow.com/a/38270173/5175709). I have never done what you are asking so don't quote me on this (I'm also a noob), but I think you can still use extensions to use a new `init` method that *while* it addresses the needs of the original class, will give you control on instantiating. See [here](http://stackoverflow.com/questions/27884380/how-to-add-initializers-in-extensions-to-existing-uikit-classes-such-as-uicolor/27884498#27884498) – mfaani Nov 13 '16 at 21:26
  • @Honey, thanks I am accepting your answer. I cannot do the initialization (from your comment I can see that I can create convenience initializers but that doesn't help me). While your answer is not what I wanted to hear, I think it is correct. The solution I have chosen is to create a set of extensions and to make minimal changes to the AWSIdentityManager to allow me to gain control during initialization. – Bruce0 Nov 15 '16 at 18:24