0

Im trying to assign a delegate of a node inside another class, but its not working.

Example:

Class A:

import SpriteKit

protocol ClassADelegate {

    func testA() -> Bool

}

class ClassA: SKSpriteNode {


} 

Class B:

 import SpriteKit

    protocol ClassBDelegate {

        func testB() -> Bool

    }

    class ClassB: SKSpriteNode {

       let aux = ClassA()
       aux.delegate = self
    } 

So my problem is when i try to initialize the aux delegate. I already tried aux.delegate = ClassADelegate but that dont work either. It says i cant Assign a value of type ClassADelegate.protocol to ClassADelegate.

Any solution? Thanks.

Ps.: This is a very simple example and i know it dont result in anything. My problem is just with the delegate declaration.

Gusfat
  • 111
  • 11

1 Answers1

1

You have a whole bunch of problems.

First, ClassA doesn't have a property called delegate. Add that:

class ClassA: SKSpriteNode {
    var delegate : ClassADelegate?
}

Second, you can't just throw code randomly in the middle of a class. It has to go in a method, so move aux.delegate = self to an initializer:

class ClassB: SKSpriteNode {

    let aux = ClassA()

    override init(texture: SKTexture!, color: UIColor!, size: CGSize) {
        super.init(texture: texture, color: color, size: size)
        aux.delegate = self
    }
}

Third, ClassB does not conform to ClassADelegate. Declare that it does:

class ClassB: SKSpriteNode, ClassADelegate {

Fourth, ClassB does not implement the methods required by ClassADelegate, in this case TestA(). So add that:

func testA() -> Bool {
    return true
}

Finally, add the required initializer to ClassB:

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

Here's the final code that will work:

protocol ClassADelegate {

    func testA() -> Bool

}

class ClassA: SKSpriteNode {
    var delegate : ClassADelegate?
}

protocol ClassBDelegate {

    func testB() -> Bool

}

class ClassB: SKSpriteNode, ClassADelegate {

    let aux = ClassA()

    override init(texture: SKTexture!, color: UIColor!, size: CGSize) {
        super.init(texture: texture, color: color, size: size)
        aux.delegate = self
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func testA() -> Bool {
        return true
    }
}
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Sorry i tried to make the code as simple as possible but i guess i made a wrong move. haha Thx =) – Gusfat Sep 12 '15 at 05:18
  • But i still have one doubt, what if i didnt want to use the testA function in the Class B and instead i wanted to use that function in another class, is that possible? Like i wanted to call funcA in my scene instead of my Class B. – Gusfat Sep 12 '15 at 05:25
  • Sure… just make your scene also conform to `ClassADelegate` – Aaron Brager Sep 12 '15 at 05:26
  • But the problem is that my ClassA will require that i implement my testA even if i dont want to since i just want to create my node, not implement all his functions. – Gusfat Sep 12 '15 at 05:31
  • Then make it optional in class A's delegate definition – Aaron Brager Sep 12 '15 at 05:35
  • How? I think that is my question after all. – Gusfat Sep 12 '15 at 05:38
  • 1
    See here: http://stackoverflow.com/questions/24032754/how-to-define-optional-methods-in-swift-protocol – Aaron Brager Sep 12 '15 at 05:39
  • Thanks a lot Aaron. Im still having some troubles with crash but i'll try to figure that out. You gave me the right path. – Gusfat Sep 12 '15 at 05:53