avery porter avery porter - 1 year ago 86
Swift Question

count integer once it's converted from string to integer?? swift 3

I'm trying to increase the integer by 1, like this

var myInt = 1
var myString = Int(myInt)

func increaseInt() {
myInt = myInt + 1
}


But I can't seem to access the integer once it's converted to a string

Answer Source

myInt is a value type and not a reference. So once you convert myInt to a string (which should be done like this: var myString = "\(myInt)") any changes made to myInt have no effect on myString.

If you want to make sure that myString always equals the string representation of myInt, you can make myString a computed property...

var myString: String {
  get {
    return "\(myInt)"
  }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download