Why can't I use self in func Swift

I'm trying to add SKSpriteNodes to a view in a function myself, but Xcode will not let me do this. This gives me the error "Using an unresolved identifier" I "

func indicate() { if test == 0 { var large = ((CGFloat(largest)*54) - 29) - selectedNode.position.x var small = selectedNode.position.x - ((CGFloat(smallest)*54) - 29) indicatorRight.position = CGPointMake(selectedNode.position.x + large, selectedNode.position.y) indicatorRight.userInteractionEnabled = true indicatorRight.zPosition = 0.5 indicatorLeft.position = CGPointMake(selectedNode.position.x - small, selectedNode.position.y) indicatorLeft.userInteractionEnabled = true indicatorLeft.zPosition = 0.5 println(indicatorLeft.position) // println(smallest) self.addChild(indicatorRight) self.addChild(indicatorLeft) } } 
+6
source share
2 answers

Make sure that the method is represented in the opening and closing brackets of the class

class A {

// You need to define a method here

}

// You could declare it here.

+18
source

Nas

For func to use "self", it must be part of the class.

"self" refers to the current instance on which the method is applied (or in Swift: "func"). If func is defined globally, then if is not part of the class, so it cannot be associated with an instance of the class.

Therefore, the inability to use the ā€œIā€ in this context.

+6
source

Source: https://habr.com/ru/post/970721/


All Articles