所以我必须将自定义类的实例从一个 UIViewController
传递到另一个:
targetVC.reservation = self.reservation!
print(self.reservation!.id, "before")
targetVC.reservation!.phoneNumber = self.phoneTextField.text!.phoneToString()
targetVC.reservation!.id = id
print(self.reservation!.id, "after")
我的问题是 self.reservation!.id
也发生了变化:“before”是 “”
,而“after"它是 id
。为什么会发生这种情况以及如何避免这种情况?
最佳答案
您可以使用 mutableCopy()
使用您的对象,但为此您的自定义类需要从 NSObject
扩展并且其返回类型为 Any
因此您需要将其结果显式类型转换为您的 CustomClass
.
targetVC.reservation = self.reservation!.mutableCopy() as! YourCustomClass
关于Swift 类属性更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41343905/