Swift3 如何在swift 3中的另一个ViewController中获取可映射的模型类数据

Swift3 如何在swift 3中的另一个ViewController中获取可映射的模型类数据,swift3,objectmapper,Swift3,Objectmapper,我有一个可映射类型的模型类,在该类中,我在firstViewController中从api传递数据。现在我想访问secondViewController中的模型类数据。那么我如何使用secondViewController中的数据。 这是我的密码: import ObjectMapper class UserProfileResponse: Mappable { var data:UserProfileData? var success: Bool? var error: String?

我有一个可映射类型的模型类,在该类中,我在firstViewController中从api传递数据。现在我想访问secondViewController中的模型类数据。那么我如何使用secondViewController中的数据。 这是我的密码:

import ObjectMapper

class UserProfileResponse: Mappable {

var data:UserProfileData?
var success: Bool?
var error: String?

required init?(map: Map){

}

func mapping(map: Map) {
    data <- map["data"]
    success <- map["success"]
    error <- map["error"]
}
}

class UserProfileData: Mappable {

var address: String?
var joinDate: String?
var phone: String?
var policyName: String?
var imageUrl: String?
var name: String?
var policyNo: String?
var title: String?

required init(map: Map){

}

func mapping(map: Map) {
    address <- map["address"]
    joinDate <- map["joinDate"]
    phone <- map["phone"]
    policyName <- map["policyName"]
    imageUrl <- map["imageUrl"]
    name <- map["name"]
    policyNo <- map["policyNo"]
    title <- map["title"]
}

}
导入对象映射器
类UserProfileResponse:可映射{
var数据:UserProfileData?
var成功:布尔?
变量错误:字符串?
必需的初始化?(映射:映射){
}
func映射(映射:映射){

数据如果使用segue从firstViewController显示secondViewController,则可以使用prepare segue传递数据

    override func prepare(for segue: UIStoryboardSegue, sender _: Any?) {
            let destinationViewController = segue.destination as! SecondViewController
 destinationViewController.userProfileData = self.userProfileData
}
然后,您的第二个ViewController将与您的实现类似

class SecondViewController{

var userProfileData:UserProfileData?
}

如果使用segue从firstViewController显示secondViewController,则可以使用prepare segue传递数据

    override func prepare(for segue: UIStoryboardSegue, sender _: Any?) {
            let destinationViewController = segue.destination as! SecondViewController
 destinationViewController.userProfileData = self.userProfileData
}
然后,您的第二个ViewController将与您的实现类似

class SecondViewController{

var userProfileData:UserProfileData?
}

创建一个Signleton类,您可以在其中从API响应填充UserProfileData

class UserProfileManager {
    static var userProfile: UserProfileData?

 private static var sharedManager: UserProfileManager = {
        let shared = UserProfileManager()
        return shared
    }()

    class func shared() -> UserProfileManager {
        return sharedManager
    }
}

您可以从此单例访问userProfileData,但只有在第一次退出并启动它时,它才会保留下来。它不会将您的信息存储在UserDefaults中。

创建一个Signleton类,您可以从API响应中填充userProfileData

class UserProfileManager {
    static var userProfile: UserProfileData?

 private static var sharedManager: UserProfileManager = {
        let shared = UserProfileManager()
        return shared
    }()

    class func shared() -> UserProfileManager {
        return sharedManager
    }
}

您可以从此单例访问userProfileData,但只有在您退出并启动它时,它才会在第一次访问时保留。它不会在用户默认值中存储您的信息。

@ASP感谢我知道,但我不会从第一个导航到第二个,也不会从第二个导航到第二个。实际上,在从菜单中选择选项后,secndVc视图会打开。因此它不在navig中ation viewControllers堆栈。所以我需要的是如何创建UserProfileData对象并使用它。@ASP感谢我知道,但我不是从第一个导航到第二个,或者从第二个导航到第二个。实际上,在从菜单中选择选项后,secndVc视图将打开。因此它不在导航viewControllers堆栈中。所以我需要的是如何创建它UserProfileData的对象并使用它。No bro,实际上我想创建UserProfileData的对象,然后我想使用从API Resose获得的存储在该类中的值。您给出的解决方案对于传递数据b/w 2控制器是正确的。No bro,实际上我想创建UserProfileData的对象,然后我想告诉我们e我从API Resose获得的存储在该类中的值。您给出的解决方案对于传递b/w 2控制器的数据是正确的。Santosh告诉我如何处理该类中的null值。我得到null作为“policyNo”的响应.Mappable如果为nil,则返回nil。如果要使用某些默认值处理nil,则可以在让policyNum=map.JSON[“policyNum”]{//assign value if not is nil}的情况下执行此操作。Santosh还有一件事我如何处理此类中的null值。我在“policyNo”的响应中得到null.Mappable如果为nil,则返回nil。如果要使用某个默认值处理nil,则可以在让policyNum=map.JSON[“policyNum”]{//assign value if not为nil}时执行此操作