I have these definition in my function which work
class MyClass {
func myFunc() {
let testStr = \"test\"
let testStrLen = countElements(t
If I understand your question correctly, you are asking how you can create class level constants (static - in C++ parlance) such that you don't a) replicate the overhead in every instance, and b have to recompute what is otherwise constant.
The language has evolved - as every reader knows, but as I test this in Xcode 6.3.1, the solution is:
import Swift
class MyClass {
static let testStr = "test"
static let testStrLen = count(testStr)
init() {
println("There are \(MyClass.testStrLen) characters in \(MyClass.testStr)")
}
}
let a = MyClass()
// -> There are 4 characters in test
I don't know if the static is strictly necessary as the compiler surely only adds only one entry per const variable into the static section of the binary, but it does affect syntax and access. By using static, you can refer to it even when you don't have an instance: MyClass.testStrLen
.
Tried on Playground
class MyClass {
struct Constants {
static let testStr = "test"
static let testStrLen = testStr.characters.count
//testInt will not be accessable by other classes in different swift files
private static let testInt = 1
static func singletonFunction()
{
//accessable
print("Print singletonFunction testInt=\(testInt)")
var newInt = testStrLen
newInt = newInt + 1
print("Print singletonFunction testStr=\(testStr)")
}
}
func ownFunction() {
//not accessable
//var newInt1 = Constants.testInt + 1
var newInt2 = Constants.testStrLen
newInt2 = newInt2 + 1
print("Print ownFunction testStr=\(Constants.testStr)")
print("Print ownFunction newInt2=\(newInt2)")
}
}
let newInt = MyClass.Constants.testStrLen
print("Print testStr=\(MyClass.Constants.testStr)")
print("Print testInt=\(newInt)")
let myClass = MyClass()
myClass.ownFunction()
MyClass.Constants.singletonFunction()
What about using computed properties?
class MyClass {
class var myConstant: String { return "What is Love? Baby don't hurt me" }
}
MyClass.myConstant
Some might want certain class constants public while others private.
private keyword can be used to limit the scope of constants within the same swift file.
class MyClass {
struct Constants {
static let testStr = "test"
static let testStrLen = testStr.characters.count
//testInt will not be accessable by other classes in different swift files
private static let testInt = 1
}
func ownFunction()
{
var newInt = Constants.testInt + 1
print("Print testStr=\(Constants.testStr)")
}
}
Other classes will be able to access your class constants like below
class MyClass2
{
func accessOtherConstants()
{
print("MyClass's testStr=\(MyClass.Constants.testStr)")
}
}
Adding to @Martin's answer...
If anyone planning to keep an application level constant file, you can group the constant based on their type or nature
struct Constants {
struct MixpanelConstants {
static let activeScreen = "Active Screen";
}
struct CrashlyticsConstants {
static let userType = "User Type";
}
}
Call : Constants.MixpanelConstants.activeScreen
UPDATE 5/5/2019 (kinda off topic but
Perhaps a nice idiom for declaring constants for a class in Swift is to just use a struct named MyClassConstants like the following.
struct MyClassConstants{
static let testStr = "test"
static let testStrLength = countElements(testStr)
static let arrayOfTests: [String] = ["foo", "bar", testStr]
}
In this way your constants will be scoped within a declared construct instead of floating around globally.
I've added a static array constant, in response to a comment asking about static array initialization. See Array Literals in "The Swift Programming Language".
Notice that both string literals and the string constant can be used to initialize the array. However, since the array type is known the integer constant testStrLength
cannot be used in the array initializer.