You write an in-out parameter by placing the inout keyword at the start of its parameter definition. An in-out parameter has a value that is passed in to the function, is modified by the function, and is passed back out of the function to replace the original value.
But how to not copy back the result if it was not changed at all
I have a database parser
which assigns the attr only when it's value changes, however, with the behavior of inout, the attr that is passed in is always set (marking my database object dirty and changed :/ )
func importStringAttribute(_ json: JSON, _ key: String, _ attr: inout String?) {
if !json[key].exists() {
return
}
if let v = json[key].string, v != attr {
attr = v
}
}
// the myDBObject.someAttr is always set
importStringAttribute(json, "someAttr", &myDBObject.someAttr)
is there a way of modification, so the value is only set when the passed in attribute really changes?