Let's say I have a structure that stores some data in its internal class. But this is exactly the structure. And if this structure is copied in the code, how can I copy the object with all the data contained in this object, at the time of assign the structure to new variable?
class Storage {
var item: String = ""
func upload(item: String) { self.item = item }
func download(key: String) -> String { item }
}
struct StackOnDisk {
let storage: Storage
init() {
storage = Storage()
}
mutating func push(item: String) { storage.upload(item: item) }
mutating func pop(key: String) -> String { storage.download(key: key) }
}
class Controller {
func work() {
var stackone = StackOnDisk()
stackone.push(item: "Maya")
var stacktwo = stackone // How to initialize new Storage at stacktwo instance at this moment?
stacktwo.push(item: "Willy")
let firstitem = stackone.pop(key: "key")
print(firstitem)
}
}
func test() {
Controller().work() // prints Willy, it is no good, it must prints Maya
}