Aej11a
Aej11a

Reputation: 253

How do I parameterize variables in Swift?

For extensibility purposes I want to parameterize certain numbers and text in my iOS app. Is there a way to do this with a Config file or similar implementation?

For example, I'm using multiplier connectivity and there are a few places in my app where the same number needs to be used, such as the maximum number of peers allowed in the connection. Is there a way to turn this in to a parameter so that I can just change it in one file?

Upvotes: 0

Views: 132

Answers (1)

Jordan Osterberg
Jordan Osterberg

Reputation: 289

There is not an official way to do this, however a common implementation in some projects is to create a Constants.swift file, where you store constant variables:

class Constants {

    static let maximumClients = 4

}

Which you can then use as Constants.maximumClients wherever desired. You could even go a step further and create different files for each "category" of constants to keep things organized:

class MultipeerConstants {

    static let maximumClients = 4

}

class UIConstants {

    static let leadingAndTrailingInsets: CGFloat = CGFloat(15)

}

Upvotes: 1

Related Questions