Swift 常量文件 - 类还是结构? [英] Swift Constants file - class or struct?

查看:25
本文介绍了Swift 常量文件 - 类还是结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 Swift 项目中创建一个常量文件 - 填充静态 let 字符串.

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?

推荐答案

通过一个类,你可以创建一个子类,并且显然override class 方法.如果您纯粹使用 static,则完全没有区别.

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.

如果属性是值类型,static if let someTypeProperty 就可以了.如果它们是引用类型,则需要格外小心.

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

只是一些带有属性的东西:

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 属性按预期工作.


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)

<小时>

我所知道的唯一防呆方法是使用 static 函数.如果你想成为,你显然可以使用 class 函数能够subclass classoverride 函数.(static 不允许覆盖,实际上是 class final 的别名)


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)

这也可以防止过多的类型属性留在内存中 没有办法摆脱 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)
    }
}

<小时>

然后由您来决定什么更有意义.由于封闭的 structclass 本身从未使用过,它使没有不同.对我来说,struct 更有意义,因为我将太多行为与 classes 联系起来.


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)

这篇关于Swift 常量文件 - 类还是结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆