Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset Struct Variable in Swift

Tags:

ios

swift

swift2

I create a struct to store Strings for the project.

Example:

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"
}

In the app, I could change these variables at some point.

I am wondering if I could reset all value back to the initial state?

like image 714
JayVDiyk Avatar asked Nov 21 '15 04:11

JayVDiyk


2 Answers

The easiest way to reset a StringStruct variable would be to assign a new default value:

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

strings = StringStruct()

If you want to do that as a (mutating) function then I would implement it as

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()
    }
}

which eliminates the need to repeat all default values.

like image 78
Martin R Avatar answered Oct 22 '22 12:10

Martin R


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"
     }
}
like image 3
tuledev Avatar answered Oct 22 '22 11:10

tuledev