0

Consider the following example

class ClassA {
    
    func createAnInstanceOfAnotherClass() -> AnotherClass {

        return AnotherClass()
    }
    
    func callMeA() {
        
    }
}

class ClassB {
    func createAnInstanceOfAnotherClass() -> AnotherClass {

        return AnotherClass()
    }
    
    func callMeB() {
        
    }
}

class AnotherClass {
    func doSomethingAndReturn() {
        return
    }
}

class MethodChain {
    func methodChainTest() {
        ClassA()
            .createAnInstanceOfAnotherClass()
            .doSomethingAndReturn() //return to ClassA
            .callMeA() // call classA callMe
        
        ClassB()
            .createAnInstanceOfAnotherClass()
            .doSomethingAndReturn() // return to ClassB
            .callMeB() // call ClassB callMe
    }
}

Is it possible for the class AnotherClass to return the instance of the class that created it? In this example I want to use the class method doSomethingAndReturn when method chaining with both ClassA and ClassB and then contione the method chain with methods from either ClassA or ClassB

sago92
  • 51
  • 6

1 Answers1

1

You could make AnotherClass generic with a type parameter Creator, which stores the type of its creator.

class ClassA {
    
    func createAnInstanceOfAnotherClass() -> AnotherClass<ClassA> {

        return AnotherClass(creator: self)
    }
    
    func callMeA() {
        
    }
}

class ClassB {
    func createAnInstanceOfAnotherClass() -> AnotherClass<ClassB> {

        return AnotherClass(creator: self)
    }
    
    func callMeB() {
        
    }
}

class AnotherClass<Creator: AnyObject> {
    // weak to avoid retain cycles!
    private weak var creator: Creator?
    
    init(creator: Creator) {
        self.creator = creator
    }
    
    func doSomethingAndReturn() -> Creator {
        // assuming you always do method chaining, 
        // and not do something weird with the intermediate results,
        // this should be safe to unwrap forcefully
        creator!
    }
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Good suggestion, but you run the risk of creating a retain cycle and possible memory leak. I would suggest making the `AnotherClass` `creator` property weak to prevent that. – Duncan C Feb 03 '21 at 14:06
  • @DuncanC Thanks for the reminder. I didn't think of that! I have edited my answer. – Sweeper Feb 03 '21 at 14:08
  • @Sweeper Does the creator have to be an optional? is there a reason for not using private weak var creator: Creator – sago92 Feb 03 '21 at 15:56
  • @sago92 weak vars must be optional. – Sweeper Feb 03 '21 at 23:54