Jay23
Jay23

Reputation: 57

How to access var of an extension in another class?

I´m working with Sprite Kit. I want to change the button picture of the main class with the settings class. How can I make the variable in the extension (from the setting class) available for the main class?

Here is the extension:

extension ChangingDots {
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)
    for touch in touches{

        let locationUser = touch.location(in: self)

        if atPoint(locationUser) == DCButton {

     var blackdot = SKSpriteNode(imageNamed: "AppIcon") //<--var I want to use


        }
    }


}
}

Here is the use in the main class:

    blackdot.setScale(0.65)
    blackdot.position = CGPoint(x: CGFloat(randomX), y: CGFloat(randomY))
    blackdot.zPosition = 1
    self.addChild(blackdot)

Does anyone have a better idea of changing button pictures of one class from another?

Upvotes: 1

Views: 1515

Answers (3)

Arrabidas92
Arrabidas92

Reputation: 1153

Sorry but you make me realize that you can't add stored properties in extensions. You can only add computed properties. You can add your blackdot var as a computed property or declare it in your main class instead of your extension. If you want to try the computed way, use this :

extension ChangingDots { 
   var blackdot:Type? { // Replace Type with SKSpriteNode type returned
       return SKSpriteNode(imageNamed: "AppIcon")
   }
   override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
     super.touchesBegan(touches, with: event)
     for touch in touches {
       let locationUser = touch.location(in: self)
       if atPoint(locationUser) == DCButton {
         blackdot = SKSpriteNode(imageNamed: "AppIcon") //<--var I want to use
       }
     }
   }
}

This way your blackdot var is only gettable and NOT settable. If you want to add the possibility to set it you need to add a setter like this :

var blackdot:Type? { // Replace Type with SKSpriteNode type returned
   get {
       return SKSpriteNode(imageNamed: "AppIcon")
   }
   set(newImage) {
       ...
   }
}

Upvotes: 0

Shawn
Shawn

Reputation: 416

Just to add on here, there are ways to "workaround" adding stored properties in an extension. Here is an article that describes how to do it: https://medium.com/@ttikitu/swift-extensions-can-add-stored-properties-92db66bce6cd

However, if it were me, I would add the property to your main class. Extensions are meant to extend behavior of a class. It doesn't seem to be the right design approach to make a main class DEPENDENT on your extension.

If you want to make it private, you can use fileprivate so your extension can access it but maintain it's "private" access. For example:

fileprivate var value: Object? = nil

Upvotes: 0

Knight0fDragon
Knight0fDragon

Reputation: 16837

If you want to use a variable in the main class, it needs to be created in the main class. Extensions are designed to extend functionality, this means functions and computed properties.

To find out more, see this documentation: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html

Upvotes: 1

Related Questions