Home » Swift » Swift: class methods may only be declared on a type

Swift: class methods may only be declared on a type

Posted by: admin January 9, 2018 Leave a comment

Questions:

I am getting the compiler error “Class methods may only be declared on a type”. I am not sure why I am getting this to be honest. All the stack overflow answers apply to another senario. Any suggestions are appreciated.

       class func fontWithSize(var size : CGFloat) -> UIFont
       {
       let font : UIFont = UIFont (name: "Roboto-Regular", size: size)!
       return font;
       }
Answers:

One thing that looks odd is the var keyword in front of the parameter name.
The other thing; are you declaring this method inside a class definition?
You’re probably getting this error because you’re declaring the method at the top-level or global scope. If that’s your intention the you don’t need the class keyword anyway.