swift3 - How to get Mappable model class data in another ViewController in swift 3 -
i had model class of mappable type in pass data api in firstviewcontroller. want access model class data in secondviewcontroller. how can use data in secondviewcontroller. here code:
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"] } }
my firstviewcontroller sending data model class
func getuserprofiledatafromapi(){ //calling method api class userprofileapi{completion in self.getprofiledata(result: completion)} } func userprofileapi(completion:@escaping (_ result:userprofiledata)->()) { let defaultobject = userdefaults.standard let headertoken = defaultobject.object(forkey: kheadertoken) as! string let headers = ["authorization": headertoken] alamofire.request(kprofileurl, method: .post, encoding: jsonencoding.default, headers: headers) .validate() .responseobject{ (response: dataresponse<userprofileresponse>) in switch response.result{ case .success: let value = response.result.value completion((value?.data)!) case .failure(let error): print(error.localizeddescription) } } } func getprofiledata(result: userprofiledata){ // here data in firstviewcontroller } how can userprofiledata in secondviewcontroller.
create signleton class can populate userprofiledata api response.
class userprofilemanager { static var userprofile: userprofiledata? private static var sharedmanager: userprofilemanager = { let shared = userprofilemanager() return shared }() class func shared() -> userprofilemanager { return sharedmanager } }
you can access userprofiledata singleton stays during first time if quit , launch wont there store info in userdefaults.
Comments
Post a Comment