Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static methods may only be declared on a type

Tags:

swift

I am getting an error that says:

Static methods may only be declared on a type

How can I solve this ?

public static func random(min min: CGFloat, max: CGFloat) -> CGFloat {

    return CGFloat.random() * (max - min) + min
}

This is how I am calling the above method from the SWIFT class.

var ran = CGFloat.random(min:-255, max:588)
like image 349
Illep Avatar asked Oct 22 '16 19:10

Illep


2 Answers

The reason I got this error message in a slightly confusing place was that I had a missing close parenthesis earlier in the class.

To the compiler the declaration seemed to be inside another method. For this reason the error message showed up in a place that seemed otherwise to be fine. Error message it self is on point.

This was the first google hit, so I'm adding my experience here in the hopes it will help others who have made a similar mess.

like image 188
Peter Lamberg Avatar answered Sep 28 '22 12:09

Peter Lamberg


I got the same error due to extra closing parentheses in the former class.

enter image description here

like image 27
Cons Bulaquena Avatar answered Sep 28 '22 13:09

Cons Bulaquena