0

While programming in Java there is common solution for cleaner code when we use static constants like this:

public class Game {
    private static final int MAX_PLAYERS_NUMBER = 1000;
}

I like very much this approach and I wanted to cast it somehow to Swift. So I started from static constants:

class Game {
    static let maxPlayersNumber = 1000
}

It looks good, but it's weird in code when I always have to use class name in non-static methods:

func doSomething() {
    print(Game.maxPlayersNumber)
}

So I thought about two approaches, one is just simple property in class:

class Game {
    let maxPlayersNumber = 1000
}

And second is use global constant in file:

let maxPlayersNumber = 1000

class Game {}

Still I'm not sure what is the best solution for using constants in methods.

Tajnero
  • 583
  • 1
  • 5
  • 12

2 Answers2

3

If you want to define global constant across the project you should use struct rather then class as Apple suggested.

struct AppConstant {
    static let constant1 = 100
}

If you want constant across the class you should just define a property with let as you have defined at last.

 class Game {

    let maxPlayersNumber = 1000

    func doSomething() {
      print(maxPlayersNumber)
    }
 }
Nikunj Damani
  • 753
  • 3
  • 11
  • I know that there is no problem with performance and memory now, but is it really good approach to load these constants everytime when creating instance of this class? I always thought that because static constants are loading only once with class is really better. I'm talking while using constants only in one class. – Tajnero Jan 16 '17 at 06:57
  • yes there will be no performance issue because xcode's current mode is constant so if you define a variable at that time first compiler consider it as a constant, if you have defined a variable and you are not mutating it compiler will give a warning that you should make it constant. So it's default mode is constant. There should be no impact on performance if you are creating constant as a property. – Nikunj Damani Jan 16 '17 at 07:05
  • Oh right, seems reasonable, I'm accepting your answer. – Tajnero Jan 16 '17 at 07:15
  • 1
    Or an `enum`: http://stackoverflow.com/questions/38585344/swift-constants-struct-or-enum. – Martin R Jan 16 '17 at 07:47
-1

You may create a new file inheriting from NSObject and declare the constants in this file as below:

public static let maxPlayersNumber = 1000

here, maxPlayersNumber can be accessed throughout your project scope in the following way :

Constants. maxPlayersNumber 

where Constants is the name of the file and maxPlayersNumber is the constant declared in the file.

Bhagyalaxmi Poojary
  • 1,213
  • 1
  • 12
  • 17
  • First of all using class name Constants for all constants would be horrible idea while creating bigger project. Secondly still I have to use class or struct name before constant. – Tajnero Jan 16 '17 at 06:51
  • Constants name was just an example for your reference. You may use anything as you want. – Bhagyalaxmi Poojary Jan 16 '17 at 06:52