Declaring private constants outside the class in Swift

When creating private constants in Swift, you can declare them inside a class,

final class SomeClass: NSObject { private let someFloat:CGFloat = 12 } 

as well as out of class.

 private let someFloat:CGFloat = 12 final class SomeClass: NSObject { } 

When an area is outside the class, this is the file in which the constant is created. Are there any other differences from using one method over another, and does anyone have any opinions on the best methods?

+6
source share
1 answer

They are addressed in different ways.

In the first case, someFloat is in the SomeClass . He gained access to SomeClass.someFloat .

In the second case, someFloat is in the module area. He only got access with someFloat .

The first method is preferred. As a rule, it is more difficult to find identifiers in the module namespace because they can be easily drowned out by all identifiers in the standard library or the / cocoa framework.

+1
source

All Articles