Swift 斯威夫特:我的命运是零

Swift 斯威夫特:我的命运是零,swift,uistoryboardsegue,Swift,Uistoryboardsegue,我让用户通过文本字段(姓名、电子邮件等)填写一些个人资料信息,这些字段用于设置我的ProfileConcoller.shared.profile值。当我到达导航段传递数据时,destinationVC.profile将不会将其值设置为发送配置文件对象,而是得到nil 我的sendingVC嵌入在导航控制器中,而我的destinationVC嵌入在选项卡栏控制器中 我的发送对象具有以下数据: (lldb)采购订单档案 配置文件:0x600000c873c0 目标对象意外为零: (lldb)采

我让用户通过文本字段(姓名、电子邮件等)填写一些个人资料信息,这些字段用于设置我的ProfileConcoller.shared.profile值。当我到达导航段传递数据时,destinationVC.profile将不会将其值设置为发送配置文件对象,而是得到nil

我的sendingVC嵌入在导航控制器中,而我的destinationVC嵌入在选项卡栏控制器中

我的发送对象具有以下数据: (lldb)采购订单档案 配置文件:0x600000c873c0

目标对象意外为零: (lldb)采购订单目的地VC?简介
nil

didSet
在您的vc尚未加载时触发,因此所有出口均为零

您需要将
updateViews()
放入
viewDidLoad


另外,您的目的地是一个选项卡栏

let tab = segue.destination as! UITabBarController
let destinationVC = tab.viewControllers![2] as! ProfileViewController

我认为您所要做的就是调用主线程中的
updateViews()

didSet {
        print("did set profile called")
        DispatchQueue.main.async{[weak self] in
            guard let self = self else {
                return
            }
            self.updateViews()
        }
    }
另一个选项是更新
viewDidLoad()中的视图


您可以使用以下代码将数据安全地传递到
ProfileViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard let profile = ProfileController.shared.profile else { return }
        if segue.identifier == "signUpMemicTBC" {
            guard let tabBarController = segue.destination as? UITabBarController else { return }
            guard let profileController = tabBarController.viewControllers?.index(at: 2) as? ProfileViewController else {
                return
            }
            profileController.profile = profile
        }
    }

在故事板(打印屏幕)上发布您的segue声明。显示属性检查器的视图控制器
ProfileController
ProfileViewController
是否相同?上面的代码片段中有拼写错误吗?好的,上面链接了图片。你能展示一下
ProfileController
ProfileViewController
的代码吗?不,ProfileController是一个模型控制器,它保存着我的单例,ProfileViewController是一个UIViewControllerthanks,我也试过了。当我在viewDidLoad后发布个人资料时,我仍然得到零如何导航
performsgue
present
?我相信performsgue,我认为我以前没有使用present做
让destinationVC=segue.destination作为!ProfileViewController
并连接crashSIGABRT-无法将类型为“UITabBarController”(0x110757300)的值强制转换为“Memic.ProfileViewController”(0x103c810e0)。2019-05-30 13:01:16.666498-0600 Memic[13037:13192113]无法将类型为“UITabBarController”(0x110757300)的值强制转换为“Memic.ProfileViewController”(0x103c810e0)。这<代码>第一个(其中:{$0是ProfileViewController})的值是否为?ProfileViewController
没有意义,它应该是一个vc搜索,我确信它是一个索引,2使用幻数是不可取的。。让我们假设其他开发人员稍后开始处理此代码,并要求将此控制器移动到其他选项卡。与
tab.viewControllers相比,我编写的代码更具动态性![2] 作为!ProfileViewController
我很确定这会导致崩溃,我不太喜欢神奇数字和强制展开。我不能冒险在我正在开发的任何产品上犯这个错误。我希望我的解释有足够的上下文来说明上述代码更具动态性和可伸缩性@sh khanno需要将代码复杂化,以备将来考虑如果vc名称发生变化,这将是一个新代码重构的规模,这是一个遵循我感觉良好的方法的建议。这取决于你到底想不想跟随。我的回答从
开始,您可以使用下面的代码将数据安全地传递到ProfileViewController
。您可以参考任何编码指南,我认为每个人都声明使用可选绑定,而不是强制展开,并避免使用幻数。
override func viewDidLoad() {
    if let prof = self.profile {
        self.updateViews()
    }
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard let profile = ProfileController.shared.profile else { return }
        if segue.identifier == "signUpMemicTBC" {
            guard let tabBarController = segue.destination as? UITabBarController else { return }
            guard let profileController = tabBarController.viewControllers?.index(at: 2) as? ProfileViewController else {
                return
            }
            profileController.profile = profile
        }
    }