Join Todd Perkins for an in-depth discussion in this video Writing the initial logic for the app, part of Building and Distributing a Simple App with Xcode and Swift.
- So let's write some of the logic for our app. I'll switch back to the Standard editor, and using the Navigator, I'll go back into ViewController.swift. Now, some of the code is going to be fairly straightforward. To make a webView goBack and forward is as simple as calling the webView's goBack and Forward methods. So inside of goBack, I'm going to call webView.goBack. And same thing in goForward, webView.goForward. Now when we tap go, there are a few different things we want to handle.
First we want to make sure that there's some text inside of the text field. So inside of didTapGo, I'm going to write that code now. So check to see if addressBar.text is equal to an empty string, If it is, we want to return. If it's not an empty string, then we want to hold that in a constant. The reason for that is addressBar.text could possibly be null, so we want to make sure that we can reference that both shorthand, and not have to worry about knowing if it's nil or not.
Now that we know that the text field is not blank, we want to hold that value inside of constant, so that we don't have to type out the full addressBar.text code, or unwrap that code since it could possibly be nil. So to do that we're going to use a guard statement. So guard let text, that's going to be a String, equal addressBar.text else return. Now, we're getting a warning here, and that's not anything to worry about, that's just saying that we haven't used this text constant just yet.
We'll use that later on. So now we haven't written all of the logic of the application, but we've made the webView goBack, goForward, and now we have the text inside of our addressBar inside of a constant that we can quickly access.
- Setting up your development environment
- Creating a new Xcode project
- Connecting UI elements
- Writing the logic for the app
- Adding icons and buttons
- Publishing an app written in Swift