swift - How to copy a struct and modify one of its properties at the same time? -


if want represent view controller's state single struct , implement undo mechanism, how change, say, 1 property on struct and, @ same time, copy of the previous state?

struct {    let a: int    let b: int     init(a: int = 2, b: int = 3) {       self.a =       self.b = b    } }  let state = a() 

now want copy of state b = 4. how can without constructing new object , having specify value every property?

note, while use placeholder values constants a , b not able construct instance of a other values placeholders. write initializer instead. may write custom method change value in struct also:

struct {     let a: int     let b: int      init(a: int = 2, b: int = 3) {         self.a =         self.b = b     }      func changevalues(a: int? = nil, b: int? = nil) -> {         return a(a: ?? self.a, b: b ?? self.b)     } }  let state = a() let state2 = state.changevalues(b: 4) 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -