Swift类属性更新 [英] Swift class-property update

查看:106
本文介绍了Swift类属性更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我必须将自定义类的实例从一个UIViewController传递到另一个:

So I have to pass instance of my custom class from one UIViewController to another:

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也被更改了:"之前"是"",而"之后"是id.为什么会发生以及如何避免这种情况?

My problem is that self.reservation!.id is also changed: "before" it is "", and "after" it is id. Why does it happen and how to avoid this?

推荐答案

您可以使用

You can use mutableCopy() with your object but for that your custom class need to extends from NSObject and its return type is Any so you need to explicitly type cast its result to your CustomClass.

targetVC.reservation = self.reservation!.mutableCopy() as! YourCustomClass

这篇关于Swift类属性更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆