-1

I've created a constant file on my application as constants.swift and I'm using struct to define constants as follows

struct moments {
    struct parameter {
        static let email = "email"
        static let me = "me"
        static let fields = "fields"
        static let params = "id, name, first_name, last_name, picture.type(large), email" 
    }

    struct title {
        static let timeLineTitle = "Moments"
        static let settingsTitle = "Settings"
        static let menuTitle = "Menu"
        static let openCamera = "Say Cheese!!! "
        static let openGallery = "Pick from Moments "
        static let addAudio = "Record Audio "
        static let addNote = "Write Note "
    }

    struct notification {
        static let notificationIdentifier = "NotificationIdentifier"
    }

    struct entity{
       static let image = "Image"
       static let note = "Note"
       static let Audio = "Audio"
    }

    struct menu {
       static let home = "Home"
       static let settings = "Settings"
       static let logout = "Logout"
    }

    struct cell {
        static let cellIdentifier = "timeLineCell"
        static let imageCell = "imageCell"
    }

}

but I've found multiple other options like using class, singleton and enum I wanted to know which is the efficient way to use constants in my app...

Community
  • 1
  • 1
Varun Kumar
  • 344
  • 2
  • 24
  • 1
    Making a separate file for constants makes sense only when the constants are grouped in that file for some logical purpose, e.g. because they represent strings in the same protocol. Creating constants simply to have all constants in one place makes the code less readable than if you define constants in files where these constants are used. – Sergey Kalinichenko Mar 07 '17 at 12:04
  • If constant it's related to an existing type it's better to create an extension and add the constant there – Adolfo Mar 07 '17 at 12:10
  • @Adolfo can you provide me an example on how to do that – Varun Kumar Mar 07 '17 at 12:43
  • 1
    Hi @VarunKumar I've add an example ;) – Adolfo Mar 07 '17 at 12:48

5 Answers5

2

Class and Singleton class for preparing constants doesn't required till you want those constants private.
You can directly create constants in your swift file as below. enter image description here

Or using Struct.

enter image description here

Both are the efficient way to create constants. Use class and Singleton when you want constants private and to be used for specified classes.
This code is in swift 2.3, it's for general idea to create constants!

Kiran Jasvanee
  • 6,362
  • 1
  • 36
  • 52
2

Class and singleton class are designed to serve different purpose. Those should be excluded when we are talking about writing pure constants. Among two left options are struct and enum. Among them enum usually used when we group similar type of constants. For example we may keep possible states of a variable in enum. Other option struct is the best to use in your case as well as in othe cases where we deal with pure constants.

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
1

As you asking for another answer for enum case. You can create enum private/public in any swift file.
As you can see here, I've created two enums. 1 for my private usage and 1 for public. enter image description here

Private won't allowed to be accessed in all classes. It will be allowed to be accessed only in inside of which class it declared.
Now I'm trying to access enums in other class as below. You can check here, I'm getting access of publicEnum but not privateEnum. enter image description here

Now let me tell you how you can access private enums on class where you declared it.
enter image description here
You can check here, In enum_privately class, you able to access both. Now based on above elaboration, you can figured it out, when and where it will be efficient to use which way based on your requirement.

Kiran Jasvanee
  • 6,362
  • 1
  • 36
  • 52
1

Here's an example about using constants related to existing types

extension Double
{
    static var pi: Double
    {
        return 3.14159265358979323846264338327950288
    }
}

print(Double.pi)
Adolfo
  • 1,862
  • 13
  • 19
1

For constants with specific values, like strings, the problem with Swift enums is that you constantly have to convert to and from the raw value. That's why I prefer using structs as you did.

In general, I think it's good practice to use struct unless you need class for a particular reason, and if it's just for holding constants there's no advantage to using class.

Putting the values into fields of a singleton just seems like extra work, unless you sometimes need to have different values, such as for testing.

Uncommon
  • 3,323
  • 2
  • 18
  • 36