Reputation: 113
I'm fairly new to programming and I'm new to Swift 2.0 so this might be a stupid question. But I have:
struct Geometry {
static let Pi : Double = 3.14159265358979323846
func get_angle(x1: Int, y1: Int, x2: Int, y2:Int) -> Double {
...
func get_angle(point1 : CGPoint , point2 : CGPoint) -> Double {
I can access Pi by doing "Geometry.Pi" and it works just fine. But for some reason I can't access the get_angle functions. I have tried "Geometry.get_angle(x1, y1: y1, x2: x2, y2: y2)" but then it complains about "extra argument 'y1' in call" I have tried to remove all the function variable names by adding _ before the declaration of the function like so:
func get_angle( x1: Int, _ y1: Int, _ x2: Int, _ y2:Int) -> Double {
and call it like: " Geometry.get_angle(x1, y1, x2, y2) " but then it complains about "Cannot invoke 'get_angle' with an argument list of type ('Int' , 'Int', 'Int', 'Int')" Why can't I do that? That's kinda the idea.. And strangely when I start to type "Geometry.get_an" the suggestion box appears and it looks wierd.. I have a photo here: https://i.sstatic.net/YaiUu.jpg If someone want to take a look. It gives me the option : Geometry.get_angle(Geometry), but Geometry isn't the variable type.
What I essentially want to do is put a number of global functions and global variables in a "constant" / "common" - file like you would in other languages.. I know that every function is basically a global function as long as it's not marked as private. I used this: Global constants file in Swift as a reference when I created Geometry..
Can anyone help me make a constants/common-file in swift 2.0 containing variables and functions?
Edit: Just to clarify, if I make a variable of Geometry I can access the function just fine, but that's not what I want. I want global functions. Also, I tried moving them out of the struct, but that same errors appear, "Cannot invoke 'get_angle' with an argument list of type ('Int' , 'Int', 'Int', 'Int')"
Upvotes: 3
Views: 2341
Reputation: 4595
You need to make the function static
:
struct Geometry {
static let Pi : Double = 3.14159265358979323846
static func get_angle(x1: Int, y1: Int, x2: Int, y2:Int) -> Double {
return 1
}
}
When function isn't static, you can call it like this:
Geometry().get_angle( ... )
...but this creates a new Geometry
structure, which is not needed in your case.
Upvotes: 4