在Swift中重置结构变量

时间:2015-11-21 04:36:26

标签: ios swift swift2

我创建了一个结构来存储项目的字符串。

示例:

struct StringStruct {
     var BUTTON_TITLE: String = "Okay"
     var CANCEL_TITLE: String = "I don't think so"
     var DECLINE_TITLE: String = "No"
     var PROFILE_TABBAR_TITLE: String = "My Profile"
}

在应用程序中,我可以在某些时候更改这些变量。

我想知道我是否可以将所有值重置回初始状态?

2 个答案:

答案 0 :(得分:8)

重置StringStruct变量的最简单方法是 分配一个新的默认值:

var strings = StringStruct()
strings.DECLINE_TITLE = "Nein"

strings = StringStruct()

如果您想将其作为(变异)函数,那么我会实现 它是

struct StringStruct {
    var BUTTON_TITLE: String = "Okay"
    var CANCEL_TITLE: String = "I don't think so"
    var DECLINE_TITLE: String = "No"
    var PROFILE_TABBAR_TITLE: String = "My Profile"

    mutating func reset() {
        self = StringStruct()
    }
}

,无需重复所有默认值。

答案 1 :(得分:2)

struct StringStruct {
     var BUTTON_TITLE: String = "Okay"
     var CANCEL_TITLE: String = "I don't think so"
     var DECLINE_TITLE: String = "No"
     var PROFILE_TABBAR_TITLE: String = "My Profile"

     mutating func reset() {
         BUTTON_TITLE = "Okay"
         CANCEL_TITLE = "I don't think so"
         DECLINE_TITLE = "No"
         PROFILE_TABBAR_TITLE = "My Profile"
     }
}