0

For the following:

struct Constants {
    static let m2Pi = CGFloat(M_PI) * 2
}

SwiftLint 0.16.1 is warning me:

warning: Legacy Constant Violation: Struct-scoped constants are preferred over legacy global constants. (legacy_constant)

Note that I need this value for UIBezierPath.init(arcCenter center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool) to get a closed arc.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • It just seems like a dumb warning to me... as a C developer, those global constants are pretty important... if you made an enum or namespace and made the constant equal to the global constant, would it complain about it in the location that you assigned the global constant? – Grady Player Jan 30 '17 at 14:10
  • 1
    @GradyPlayer Code quality is important. It's not a dumb warning. Note that `M_PI` is actually a macro, therefore in C it can be converted to any type (it's a literal). In Swift, you have to cast it therefore it's safer to use an already given constant with that specific type. Namespacing it into a structure and removing `M_` prefix that is not needed (it's not a global anymore) just makes your code more consistent. – Sulthan Jan 30 '17 at 14:13

1 Answers1

6

In swift 3, pi is now defined this way:

CGFloat.pi

You can also get it this way:

Double.pi 
Float.pi
Ocunidee
  • 1,769
  • 18
  • 20