Skip to content

Instantly share code, notes, and snippets.

@ronaldmannak
Created September 28, 2014 23:21
Show Gist options
  • Save ronaldmannak/b2a126fe746732a730f9 to your computer and use it in GitHub Desktop.
Save ronaldmannak/b2a126fe746732a730f9 to your computer and use it in GitHub Desktop.
Trying to solve conflicting names of functions and properties in Swift. How do I call the function position() from within a class that has a property with the same name?
import SpriteKit
func position(number: Int)->Int {
return number
}
class node: SKNode {
// Note: SKNode has a property called position (a CGPoint)
func test() {
// Attempting to call function position
let i = position(2) // Compiler Error: not identical to CGPoint
let i: Int = position(2) // Nope, doesn't work either
}
}
@NatashaTheRobot
Copy link

You can also extend SKNode:

extension SKNode {
    func position(number: Int)->Int {
        return number
    }
}


class node: SKNode {

    // Note: SKNode has a property called position (a CGPoint)

    func test() {
        // Attempting to call function position
        let i = position(4)
    }
}

@NatashaTheRobot
Copy link

Is your function just a global function, or is it encapsulated in any way? If it's encapsulated, you can use the namespace it's in:

class MyCustomClass {
    class func position(number: Int)->Int {
        return number
    }
}


class node: SKNode {

    // Note: SKNode has a property called position (a CGPoint)

    func test() {
        // Attempting to call function position
        let i = MyCustomClass.position(2)

    }
}

@ronaldmannak
Copy link
Author

@NatashaTheRobot it is a global function

@NatashaTheRobot
Copy link

You should be able to call it as Swift.position(2), but it's not working...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment