Swift Constants file - class or struct?

With a class you can create a subclass and obviously override class methods. If you are purely using static there is no difference at all.

If the properties are Value Types, static if let someTypeProperty will be fine. If they are Reference types some extra care is needed.


Just some stuff with properties:

struct PresetStringsStruct {

    static let someString : String = "Some Text" // struct
    static let someView : UIView = UIView(frame: CGRectZero)

    private init () {
        print("init") // never happens
    }
}

class PresetStringsClass {

    static let someString : String = "Some Text" // struct
    static let someView : UIView = UIView(frame: CGRectZero)

    private init () {
        print("init") // never happens
    }
}

struct properties work as expected.

// value properties
var txtStruct = PresetStringsStruct.someString // "Some Text"
txtStruct = "SomeOtherText" // "SomeOtherText"
var txtStruct2 = PresetStringsStruct.someString // "Some Text"

var txtClass = PresetStringsClass.someString // "Some Text"
txtClass = "SomeOtherText" // "SomeOtherText"
var txtClass2 = PresetStringsClass.someString // "Some Text"

When the property is a reference type the static properties will return references to one instance.

// reference properties
var viewStruct = PresetStringsStruct.someView
viewStruct.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
var viewStruct2 = PresetStringsStruct.someView // CGRect(x: 0, y: 0, width: 50, height: 50)

var viewClass = PresetStringsClass.someView
viewClass.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
var viewClass2 = PresetStringsClass.someView // CGRect(x: 0, y: 0, width: 50, height: 50)

The only fool proof method that I know of is to use static functions. You can obviously use class functions if you want to be able to subclass the class and override the functions. (static does not allow override and is actually an alias for class final)

This also prevents too many Type Properties from remaining in memory There is no way to get rid of a static let someProperty : Int = 0

struct PresetStringsStruct {

    static func someStringFunc() -> String {
        return "SomeText"
    }

    static func someViewFunc() -> UIView {
        return UIView(frame: CGRectZero)
    }
}

class PresetStringsClass {

    static func someStringFunc() -> String {
        return "SomeText"
    }

    static func someViewFunc() -> UIView {
        return UIView(frame: CGRectZero)
    }
}

Then it is up to you to decide what makes more sense. Since the enclosing struct or class is never used itself, it makes no difference. For me a struct makes more sense because I associate too much behaviour with classes.


You can also give yourself more work and get rid of the () that results from using functions instead of properties.

struct PresetStringsStruct {

    static var someString : String {
        get {
            return someStringFunc()
        }
    }

    static var someView : UIView {
        get {
            return someViewFunc()
        }
    }

    static func someStringFunc() -> String {
        return "SomeText"
    }

    static func someViewFunc() -> UIView {
        return UIView(frame: CGRectZero)
    }
}

var viewStruct = PresetStringsStruct.someView
viewStruct.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
var viewStruct2 = PresetStringsStruct.someView // CGRect(x: 0, y: 0, width: 0, height: 0)

Tags:

Xcode

Swift