简体   繁体   English

Swift 常量文件 - 类还是结构?

[英]Swift Constants file - class or struct?

I want to create a Constants file in my Swift project - filled with static let strings.我想在我的 Swift 项目中创建一个常量文件 - 填充静态 let 字符串。

Should I create as a struct or a class?我应该创建一个结构体还是一个类? And why?为什么?

With a class you can create a subclass and obviously override class methods.使用类,您可以创建子类并显然override class方法。 If you are purely using static there is no difference at all.如果您纯粹使用static ,则完全没有区别。

If the properties are Value Types, static if let someTypeProperty will be fine.如果属性是值类型, static if let someTypeProperty就可以了。 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. struct属性按预期工作。

// 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 type ,静态属性将返回对一个实例的引用。

// 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.我所知道的唯一防呆方法是使用static函数。 You can obviously use class functions if you want to be able to subclass the class and override the functions.如果您希望能够对subclass进行subclass classoverride函数,则显然可以使用class函数。 ( static does not allow override and is actually an alias for class final ) static不允许覆盖,实际上是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这也可以防止过多的类型属性保留在内存中没有办法摆脱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.由于封闭structclass本身从未使用过,因此没有区别。 For me a struct makes more sense because I associate too much behaviour with classes .对我来说, struct更有意义,因为我将太多行为与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)

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 .要全局访问数据,只需调用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有关更多详细信息:结构和类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM