Instance variables and methods are vital. This video shows how to set up the instance variables and methods for the calculator app we're creating in this chapter.
- [Instructor] With our user interface set up, we're ready to start writing the code for our application. So I'm going to head over to ViewController.swift and then I'm going to start creating some instance variables and defining some methods. I'm going to start underneath my import statement to define the enum modes to hold all of our data types. So I'll type enum, and then I'm going highlight Swift Enumerated Type Declaration and press the Return key. I'll call this modes. Click away to hide the hinting.
Then tab over to the case value right here. The first case I'm going to define is not set, not_set. I'll go to the next line, define a new case. This is called addition, and then the final mode we're going to do now is subtraction, so case space subtraction. So we have the modes for our calculator. Let's scroll down into our ViewController class, and we're going to create some instance variables. So I'll put the cursor right underneath my class declaration, tab in, go down a few lines, go up one, and there we are.
Here I'm going to declare label string, so var labelString, and it's going to be a string. I'll set its value to "0". On the next line, we're going to have a variable called currentMode, and it's going to of the type modes that we defined just a minute ago. And we'll initialize that to .not_set. You can also type it the long way, which is modes.not_set, but personally, I prefer the shorthand way of just .not_set.
Let's go to the next line, and then I'm going to define var space savedNum with a capital "N", data type it to an int, so :Int with a capital "I", set it equal to zero. And then our last instance variable here is going to be called lastButtonWasMode. So var space lastButtonWasMode. This is going to be a Boolean, so :Bool, capital B, and then set that equal to false. Now we've set up our initial instance variables here, and then we're going to define some methods.
So let's scroll down a little bit, below all of the other existing code, and I'm going to tab in from one of the bottom lines, get some returns in there to get some more space, and then I'm going to define a few methods here. Number one is going to be update text, func space updateText with a capital "T" some parentheses, open curly brace, and just a return will add that close curly brace. Below updateText, I'm going to declare changeMode.
So func changeMode, and some parentheses, and some curly braces. So open curly brace, and then a return to write out the closed curly brace. Now this method is actually going to receive the new mode. Remember it's going to be triggered when a plus or minus button is tapped. And so it's going to send information from those handler methods so when we tap the plus button, it's going to run didPressPlus, and then plus is going tell changeMode that that button was pressed.
So it needs to receive the new mode to change to. So we'll call that newMode, so that's going to be its parameter name. It's of the data type modes. Remember we just declared that earlier. And that completes our initial properties and methods. And remember this is not include the instance variables that we're going to be connecting from our user interface, or the methods we'll connect from our user interface as well. This simply creates all of the variables and methods that we have to create by hand.
- Installing Xcode
- Creating an Xcode project
- Configuring the iOS Simulator
- Understanding variables
- Connecting visual objects to variables
- Understanding methods
- Connecting a button to a method
- Using conditional statements
- Setting up the user interface (UI)
- Building a complete iOS app