-2

Hi all am getting a data from server i.e:

{


  error = 0;

  "error_msg" = "Successfully Login";

 success = 1;

  user =     {

  DriverID = 35;

 DriverName = "Home  nath";

"com_id" = 2;

};

}

now i need to pass data of DriverName to other view controller in label box.how to do this one??

Sailendra
  • 1,318
  • 14
  • 29
antonyraja
  • 13
  • 7
  • It's more helpful if you post what you already tried in order to solve it and what error you're getting. – brduca Nov 10 '16 at 11:52
  • i have almost done with calling url printing url and object getting from the server..now i cant able to print particular object.for that i have tried: var result = jsonResults["error_msg"] as? String print("rest:\(result)") – antonyraja Nov 10 '16 at 11:56
  • you can upvote my answer if it helps you:-) – Ashok R Nov 10 '16 at 12:37

1 Answers1

2

create a variable of type string in your destination view controller.

DestinationViewController.Swift

class DestinationViewController: UIViewController {

    var driverName: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        print(driverName)

    }
}

CurrentViewController.swift

In your CurrentViewController override prepareForSegue:sender: method.

class CurrentViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destnationVC = segue.destination as! DestinationViewController
        destnationVC.driverName = "Home nath"
    }

}

learn how to pass data from CurrentViewController to DestinationViewController and DestinationViewController to OrignViewController it ll help you lot.

refer Passing Data between View Controllers.

Community
  • 1
  • 1
Ashok R
  • 19,892
  • 8
  • 68
  • 68
  • i want to fetch key option not particular name like homenath – antonyraja Nov 10 '16 at 12:42
  • so you have to parse the data first. – Ashok R Nov 10 '16 at 12:43
  • first help me print driver name in same controller....then i will parse that to other controller....thanks – antonyraja Nov 10 '16 at 12:46
  • var result = jsonResults["DriverName"] as? String print("rest:\(result)") i tried this but it printing as nil value – antonyraja Nov 10 '16 at 12:49
  • Your json data is not in correct format. you can validate your json here. http://codebeautify.org/jsonvalidator – Ashok R Nov 10 '16 at 12:52
  • First you have to convert it to dictionary object using `let jsonDataDict = try JSONSerialization.jsonObject(with: jsonData as Data, options: JSONSerialization.ReadingOptions(rawValue: 0)) as? NSDictionary` – Ashok R Nov 10 '16 at 12:56
  • `do { let jsonDataDict = try JSONSerialization.jsonObject(with: jsonData as Data, options: JSONSerialization.ReadingOptions(rawValue: 0)) as? NSDictionary //Work with your json dictionary. } catch let error as NSError { print(error) }` – Ashok R Nov 10 '16 at 12:57