Swift constants file - class or structure?

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

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

+5
source share
2 answers

With a class, you can create a subclass and, obviously, override class methods. If you use only static , there is no difference.

If the properties are value types, static if let someTypeProperty will be fine. If they are link types, additional help is required.


Just some things 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 a property is a reference type , static properties return references to a single 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 I know of is by using static functions. . You can use class functions if you want to be able to subclass class and override functions. ( static does not allow overriding and is actually an alias for class final )

It also prevents the storage of too many type properties in memory . Unable to get rid of 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's up to you to make more sense. Since closing struct or class never used on their own, it does not matter. For me, a struct makes more sense, because I associate too much behavior with classes .


You can also give more work and get rid of () , which 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) 
+5
source

You can use structs for static data - the best choice:

 struct staticStrings { static let name = "String1" static let age = "age1" } 

And to access data all over the world, just call staticStrings.name .

Why we use structs better than a class: Structures are preferable if they are relatively small and flexible, because copying is safer than plural reference to the same instance as classes.

Read More: Structures and Classes

+1
source

Source: https://habr.com/ru/post/1236681/


All Articles