Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Constants file - class or struct?

Tags:

xcode

swift

I want to create a Constants file in my Swift project - filled with static let strings.

Should I create as a struct or a class? And why?

like image 777
YogevSitton Avatar asked Nov 24 '15 08:11

YogevSitton


2 Answers

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)
like image 148
R Menke Avatar answered Dec 20 '22 05:12

R Menke


You may use structs for static data is the best choice :

struct staticStrings {

    static let name = "String1"
    static let age = "age1"

}

And to access the data globally just call staticStrings.name.

Why we use structs and its better than class: Structs are preferable if they are relatively small and copiable because copying is way safer than having multiple reference to the same instance as happens with classes.

For more details: structs and classes

like image 43
Nata Mio Avatar answered Dec 20 '22 05:12

Nata Mio