D. Murphy D. Murphy - 1 month ago 11
Swift Question

Initializer for conditional binding must have Optional type, not '[String]' - Xcode 8.0 Swift 3.0

I am creating a function that allows me to create a array on VC1 and then I can transfer over the array using "prepare for segue" to VC2. On VC2 I can append an item to the array in VC2 and then transfer over the array back to VC1. My issue is, is that on the line

if let newString = receivedString
(VC1) and erro is comming up that states, "Initializer for conditional binding must have Optional type, not '[String]'"This is my code on VC1:

var receivedString = [String]()

override func viewDidAppear(_ animated: Bool) {

if let newString = receivedString {
print(newString)
}
}


This is my code on VC2:

let stringToPass = "Hello World"

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! ViewController
destinationVC.receivedString = [stringToPass]
destinationVC.receivedString.append("DYLAN MURPHY")
}


I am new to Swift so I realise that I may be completely wrong so I appreciate any help that leads me closer to this goal.

DRM DRM
Answer

I would personally do what you want to do a little bit differently. In VC1 I would add this code:

var username = [String]()

In VC2:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toSecondViewController" {

        let hello = segue.destination as! ViewController

        hello.username.append(textField.text!)




    }
}