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()
?
Maybe something like this?
extension String {
mutating func trim() {
self = self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
}
Then you can use it as name.trim()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With