Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift Swfit如何用promise初始化变量?_Swift - Fatal编程技术网

Swift Swfit如何用promise初始化变量?

Swift Swfit如何用promise初始化变量?,swift,Swift,我有一个viewModel,它通过API调用获取用户配置文件。我想在viewModel中设置变量,以便在其他地方使用该变量。代码如下: class UserProfileViewModel { let userId: String private let getUserProfile: GetUserProfile init(getUserProfile = GetUserProfile()) { self.getUserProfile = getUserP

我有一个viewModel,它通过API调用获取用户配置文件。我想在viewModel中设置变量,以便在其他地方使用该变量。代码如下:

class UserProfileViewModel {

   let userId: String

   private let getUserProfile: GetUserProfile

   init(getUserProfile = GetUserProfile()) {
       self.getUserProfile = getUserProfile
       updateData()
    }

func updateData() {
    getUserProfile.start().done {
        [weak self] userProfile in
        guard let userProfile = userProfile else { return }
        // Should assign the userProfile.id to self.memberId
    }
}
它将从初始值设定项返回的errro返回给我,而不初始化所有存储的属性

我需要在init中初始化公共变量,但实际数据是从API调用中获取的,有什么方法可以实现这一点吗?

将其设置为可选(并更新所有依赖代码),如

class UserProfileViewModel {

   var userId: String?
...