Reputation: 2832
I want to change the value of a var in a swift file that i create:
class functions {
var DataSent = "Sensor"
func setValue (DataSent: String) {
self.DataSent = DataSent
}
func getValue () -> String {
return self.DataSent
}
}
when i call setValue the DataSent doesn't change what can i do?
i call it like that:
functions().setValue(stringData)
and then i call it to another class with getValue
Upvotes: 0
Views: 72
Reputation: 2246
You're creating a new instance of functions
each time you call functions()
. It's best to use a struct
with static
functions and static variables in situations like this.
struct functions {
static var DataSent = "Sensor"
static func setValue (DataSent: String) {
self.DataSent = DataSent
}
static func getValue () -> String {
return self.DataSent
}
}
print(functions.DataSent)
functions.setValue(DataSent: "Blaah")
print(functions.DataSent)
Upvotes: 4