0

I want to send json array to another view controller and display it in different labels in another view controller ??

var sendData = [String: AnyObject]()
sendData = datas
print("Send Data : \(sendData)")
DispatchQueue.main.async {
   self.performSegue(withIdentifier: "checkLoginViewController", sender: sendData)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destinationVC = segue.destination as? signUpViewController {
            if let sendmyArray = sender as? [String]{
                destinationVC.myarray = sendmyArray
            }
}

signUpViewController.swift:

var myarray = [String]()
var myemail = ""
var myname = ""

override func viewDidLoad() {
    super.viewDidLoad()
    MynameLabel.text = myname
    nameTextfield.text = myemail
    myarray = [myemail,myname]
    print(myarray)        
    print(myemail)
    print(myname)
}

i am able to fetch the array but the i am not able to send the array to another view controller and the i have to display it in different label

5
  • 2
    you are overriding it straight away myarray = [myemail,myname] Commented Apr 5, 2018 at 7:28
  • so how i can display it in both textfields "MynameLabel" and "nameTextfield" is my method is wrong ?? @Scriptable Commented Apr 5, 2018 at 7:30
  • I have no idea what you mean. should the array contain the JSON data or the values from the two textfields? Commented Apr 5, 2018 at 7:32
  • ok if i want to pass two data to next view controller ? and after passing it to next view controller i want to display it on different label how i can do that Commented Apr 5, 2018 at 7:35
  • array contains two data [name:sandesh, email:[email protected]] Commented Apr 5, 2018 at 7:35

1 Answer 1

1

You are mixing up array and dictionary

  • sendData is dictionary ([String: AnyObject])
  • myarray is array ([String])

Of course optional downcast sender as? [String] fails because the sender is a dictionary.

As the source is a dictionary with String keys and values change the code to

var sendData = [String: String]()

...

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if let destinationVC = segue.destination as? signUpViewController,
      let sendmyDictionary = sender as? [String: String] { 
         destinationVC.myDictionary = sendmyDictionary
   }
}

And in the second controller

var myDictionary = [String: String]()

override func viewDidLoad() {
    super.viewDidLoad()

    MynameLabel.text = myDictionary["name"]
    nameTextfield.text = myDictionary["email"]
}

Note : An unspecified value type dictionary in Swift 3+ is [String:Any] and according to the naming convention variable names are supposed to start with a lowercase letter.

Sign up to request clarification or add additional context in comments.

2 Comments

but i want to pass two data to another view controller and in next view controller i want to display it in textfield for eg:FirstVC : name: sandesh email: [email protected] these are the data i am getting through json now i want to send these data and want to display it in textfields
sorry for replying late ... it worker thank so very much @vadian

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.