Home > Net >  Why Global API response is showing nil in swift
Why Global API response is showing nil in swift

Time:04-01

I made getUser service call globally but when i call that method in required viewcontroller then data coming nil.. why?

code: created user_details API service call globally.. here UserDetailsModel is JSON response model for user_details API. when i give breakpoint then GET_USER is called and response also coming globally

 import UIKit

// to get user

var GET_USER = UserDetailsModel(dictionary: NSDictionary())

func getUser(vc: UIViewController) {

APIReqeustManager.sharedInstance.serviceCall(param: nil, method: .post, vc: vc, url: CommonUrl.user_details, isTokenNeeded: true){ [] (resp) in
            
    GET_USER = UserDetailsModel(dictionary: resp.dict as NSDictionary? ?? NSDictionary())
}
}

when i am calling getUser in EditProfileVC then breakpoint not going inside GET_USER{ didSet{ why?

how to show global user_details API response in EditProfileVC. please guide me.

 import UIKit

 class EditProfileVC: UIViewController {

private var getUserData = GET_USER{
    didSet{// breakpoint not hitting inside why?
        
        locationTf.text = getUserData?.result?.userData?.lives_in
        dobTf.text = getUserData?.result?.userData?.dob
        educationTf.text = getUserData?.result?.userData?.education
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    
    getUser(vc: self)
}
}

CodePudding user response:

private var getUserData = GET_USER{
change to
public var getUserData = GET_USER{

GET_USER = UserDetailsModel(dictionary: resp.dict as NSDictionary? ?? NSDictionary())
change to
vc.getUserData = UserDetailsModel(dictionary: resp.dict as NSDictionary? ?? NSDictionary())

CodePudding user response:

This is because you are not setting getUserData in the view controller.

you should return a value from your getUser() function like

func getUser() -> UserDetailsModel

and in your viewDidLoad

getUserData = getUser(vc: self)
  • Related