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.

2 Answers 2

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)
    }
 }
4
  • 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, 2017 at 6: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. Jan 16, 2017 at 7:05
  • Oh right, seems reasonable, I'm accepting your answer.
    – Tajnero
    Jan 16, 2017 at 7:15
  • 1
    Or an enum: stackoverflow.com/questions/38585344/….
    – Martin R
    Jan 16, 2017 at 7: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.

2
  • 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, 2017 at 6:51
  • Constants name was just an example for your reference. You may use anything as you want. Jan 16, 2017 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.