Search code examples
swiftswift-playgrounddelegation

Testing Delegation in Playground giving 'nil'


I have the following code in Playground -I'm learning delegation-...

import UIKit

protocol FollowThisProtocol {

    func passingTheValue(aValue: String)
}


class IPassTheValues{

    var aDelegate: FollowThisProtocol!

    func runThisFunc(){

       aDelegate.passingTheValue(aValue: "I like this game")

    }

}


class IReceiveTheValues: FollowThisProtocol{

    var localString: String!
        var instanceOfClass: IPassTheValues!

    func runReceivefunc(){

            instanceOfClass.aDelegate = self

    }

    func passingTheValue(aValue: String) {
        localString = aValue

    }

}

When I attempt to

print(IReceiveTheValues().localString)

it's giving me nil

It also gives me nil if I run the following lines before attempting to print(IReceiveTheValues().localString)...

IPassTheValues()   

IReceiveTheValues()

could you please help me understand why the value is not being passed from the 1st class to the 2nd..?

Or if you can spot something in my code that is contradicting itself, could you please point it out..?

Appreciate your time and help.


Solution

  • You need to create the IPassTheValues object before assigning yourself as the delegate, and then call runThisFunc() on the instance:

    func runReceivefunc(){
    
        instanceOfClass = IPassTheValues()
        instanceOfClass.aDelegate = self
        instanceOfClass.runThisFunc()
    
    }
    

    Then test:

    // Create the `IReceiveTheValues` object
    let irtv = IReceiveTheValues()
    
    // Run the method
    irtv.runReceivefunc()
    
    // Get the resulting string
    print(irtv.localString)
    

    I suggest 2 other changes. Make your delegate weak so that you don't get a retain cycle which makes it impossible to delete either object. In order to do that, you will need to add : class to your protocol declaration because only reference objects (instances of a class) can be weak.

    Here's the modified code. Try it and see what happens when you delete weak.

    protocol FollowThisProtocol: class {
        func passingTheValue(aValue: String)
    }
    
    class IPassTheValues{
        weak var aDelegate: FollowThisProtocol!
    
        func runThisFunc(){
            print("Calling delegate...")
            aDelegate.passingTheValue(aValue: "I like this game")
        }
    
        deinit {
            print("IPassTheValues deinitialized")
        }
    }
    
    
    class IReceiveTheValues: FollowThisProtocol{
        var localString: String!
        var instanceOfClass: IPassTheValues!
    
        func runReceivefunc(){
            instanceOfClass = IPassTheValues()
            instanceOfClass.aDelegate = self
            instanceOfClass.runThisFunc()
        }
    
        func passingTheValue(aValue: String) {
            print("Receiving value from helper object...")
            localString = aValue
        }
    
        deinit {
            print("IReceiveTheValues deinitialized")
        }
    }
    
    func test() {
        let irtv = IReceiveTheValues()
        irtv.runReceivefunc()
        print(irtv.localString)
    }
    
    test()