passing text between controllers as a segue

Hi,


I can't understand what's not working. But i'm workign through a tutorial trying to send text from a text field to another controller and when I click the button to sned, the programme crashes and the message is Coding is:


class ViewController: UIViewController {

@IBOutlet weak var textToSendField: UITextField!

override func viewDidLoad() {

super.viewDidLoad()

/

}

@IBAction func showMe(sender: AnyObject) {

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

/

}

/


/

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

let messageController = segue.destinationViewController as MessageViewController

messageController.messageData! = textToSendField.text?

}


The error messgae is:


: 'UIViewController' is not convertible to 'MessageViewController'; did you mean to use 'as!' to force downcast?


if I change it to as! then I get the following error


Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?


Not quite sure what's going on, can anyone help?


R

It seems you are using an old outdated tutorial. The code you have shown above looks like an old-fashioned Swift 1.1 style.

(Swift 1.2 was a big change, and the chage in Swift 2.0 was bigger. And now Swift is getting near 2.1.)

And another important thing is you don't understand the error message about optionalities. How to treal Optionals is one of the most basic (and also the most difficult) parts in learning Swift.

Without knowing how Optionals work in Swift, and just getting told how you can fix that single line, you might need to ask hundreds of questions about most code snippets in your tutorial.


This is just my recommendation and you can expect some other would tell you that single line fix,

but you should better find an updated tutorial which describes well about Optionals before writing about segues.

Thanks OOPer. I did kind of realise that there might be a fundamental gap in my knowledge that means I can't solve this. Better to take a few steps back to be able to progress.

The basic parts of programming language may seem to be so huge for starters, that many skip them and rush into some working examples.

But eventually they will find they need to learn such parts again, or would become sort of "give-me-copyable-code" people.


Swift Optionals are described here, though I'm not sure it can be a good tutorial.

(I recommend you read The Swift Programming Language (Swift 2) thoroughly, not meaning you need to have all the description in mind, but you better know where you can find what you need.)

Start Developing iOS Apps (Swift) would be another good resource which includes many tips to use Xcode effectively, though, as for now, it has some flaws.

And if you are using a paper-printed tutorial book, you might find the latest up-to-date information on the web.


Good luck, and do not hesitate to ask again in the dev forums.

passing text between controllers as a segue
 
 
Q