-2

I am trying to return the "cardsarray" which contain the name, designation, and 4 more fields. When API is being called it returns the array of object which gets stored into my array.

But when I run the application doesn't show anything in the table view.

I tried some print statement. It came out that the function is returning before the API is being called. Not able to understand why. Please help

enter image description here

 {
    designation = "Manager";
    email = "Sample@gmail.com";
    mobile =1234567899;
    name = "Sample";
    status = 0;
    Company = "ABC Corp"

},
    {
    "pending_staff_pic" = 1;
}

1 Answers1

2

As suggested by Larne, you need to understand the asynchronous calls.

First you need to have a completion block in your function like this :

func getCards(completion: @escaping (([Cards]) -> Void)) {

}

The after your for loop where you are printing cardsArray you need to write this completion block.

completion(dataArray)

And this is how you have to call this fucntion :

getCards { (cardsArray) in

}
Sharad Chauhan
  • 4,821
  • 2
  • 25
  • 50