我将自定义类的值分配给另一个变量。更新新变量的值会影响原始变量的值。但是,我需要阻止引用变量更新原始变量。
这是正在发生的事情的基本表示:
var originalVariable = CustomClass()
originalVariable.myProperty = originalValue
var referenceVariable = originalVariable
referenceVariable.myProperty = updatedValue
print("\(originalVariable.myProperty)") //this prints the ->updatedValue<- and not the ->originalValue<-
我尝试将 referenceVariable 包装在一个结构中以使其成为值类型,但它并没有解决问题。
我找到了有关值和引用类型的信息,但我无法找到解决方案。
简而言之,我的问题是:如何停止对引用变量的更新,使其不再更新为其分配值的原始变量?
提前致谢。