-1

I want to call a function within another class . But they do not return dictionary in swift. How to solve this?

In my ViewController

var dict = NSDictionary()
dict = temp.GetStation(myUrl)

In my class

func GetStation(url : String) -> NSDictionary {

    let dicts = NSDictionary()
    getResonse(url, completionhandler: { (dict) -> NSDictionary in
    //  print(dict)

        return dict                    
    })

    return dicts;
 }
Chaman sharma
  • 41
  • 1
  • 2
  • 8

1 Answers1

0

Try this code.

func GetStation(url : String, completionHandler: (stationDictionary: NSDictionary) -> NSDictionary ) {

        getResonse(url, completionhandler: { (dict) -> NSDictionary in
            //  print(dict)

            completionHandler(stationDictionary: dict)
        })

   }

and use it like,

var dict = NSDictionary()
temp.GetStation("your url") { (stationDictionary) -> NSDictionary in
            dict = stationDictionary;
            print("your dictionary := \(stationDictionary)")
        }

We have added completionHandler instead of return value, because in function GetStation you are calling function getResonse which is asychronous, may be calling webservice.

So the response of GetStation depends on the response of getResonse.

Hitendra Solanki
  • 4,871
  • 2
  • 22
  • 29