Swift. Replacing a variable's value using an extension function
swift
Solution
Maybe something like this?
extension String {
mutating func trim() {
self = self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
}
Then you can use it as `name.trim()`
Problem
I want to make a function to change the original value of my variable. for example ``` class Something { var name:String = " John Diggle " name.trim() print(name) // prints out " John Diggle " // what I wanna do is to make it so that I don't do this name = name.trim() print(name) // prints out "John Diggle" } extension String { func trim() -> String{ return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) } } ``` is there a way to change the value of a variable inside a function without doing `name = name.trim()` ?