PAGE 1
iOS App Development
Variables & Assignments
_____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC
PAGE 2
Learning Outcomes By the end of this unit, students should be able to 1. Explain what it means to assign a variable or constant. 2. Explain the purpose of a variable and constant in the program’s workflow. 3. Differentiate between a variable and a constant. 4. Use Camel Casing in development. 5. Read and write variables and constants in Xcode.
Vocabulary Assign Variable Constant Camel Casing
_____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC
PAGE 3
Assigning a Value to a Variable or Constant Now that you know how to write a very simple program, compile, and run it, let’s look at some ways to do some more interesting things. Let’s assume that we want our program to be able to remember a number and then retrieve that number later. We do this by creating a variable or constant. The Envelope Analogy Suppose we want to remember the number of states in the United States, the value of Pi, and one of my friend’s first name. We could grab 3 envelopes and label each envelope. Then on a post-it note, we could write down the values we want to remember and then put the post-it note in the envelope. Then, later on, when we want to go back and ask the question, “How many states are in the United States”, we just find the envelope with the proper label, look inside and grab the post-it note which has the answer. In our analogy, a variable is our an envelope with a name on it, telling us what is stored inside. The Post-It note is the value itself that is remembered. Putting the Post-It note into the envelope is equivalent to giving the variable a value. Looking inside and getting the Post-It note is equivalent to reading the value.
_____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC
PAGE 4
Variables and constants allow us to store (or remember) numbers and words and retrieve them later when we need them, just like items in an envelope. The way a computer does this is by storing these things in its memory. It’s memory is just like your brain. Your age is something you remember. It is stored somewhere in your brain. So, we need to be able to create that storage within the program. Variables and constants do that. So, what’s the difference between a variable and a constant? Variables are values that can change, while c onstants are values that _____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC
PAGE 5
cannot. Here are some examples of what they look like: var numberOfPeopleComingToParty = 50 let numberOfPresents = 10 var and l et are the type
numberOfPeopleComingToParty is the name
= is the operator that tells the computer that the name on the left gets the value on the right
Variables var numberOfPeopleComingToParty = 50
A variable is a value that can change. In the above example, we might want the number of people coming to the party to change. So, we indicate it by adding var as a prefix. This means that later in the code, we could alter the number to be 45 or 60. If we built an application that needed the number to change, var would do the trick. If we go back to the envelope analogy, this means that the envelope is numberOfPeopleComingToParty and 5 0 is what’s on the sticky note. var tells us that the number 50 can be changed (think of taking the sticky note out of the envelope and putting a new number in). In Swift, Xcode is smart enough to recognize that in the above example, the variable numberOfPeopleComingToParty is an Int. However, we can also declare the type explicitly in the code: _____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC
PAGE 6 var numberOfPeopleComingToParty : Int = 50
This is important to know because when we start to declare other things, like dictionaries, we will need to know how to explicitly declare the type. So, the following two sets of codes will yield the same results. The latter just explicitly declares the value type. var numberOfPeopleComingToParty = 50 var numberOfPeopleComingToParty : Int = 50
Constants let numberOfPresents = 10
A constant is a value that cannot change. In the example above, the number of presents cannot change. It must always be 10. Never 9, never 11, but always 10. We indicate it’s a constant by adding l et as a prefix. This means that later in the code, this number cannot change. It’s constant. If we go back to the envelope analogy, this means that the envelope is numberOfPresents and 1 0 is what’s on the sticky note. let tells us that the number inside that envelope can never change. It can be read, but it’s stuck in there and cannot be removed. NOTE: You must always use either a var or a let when declaring a new variable. Print to Console In Xcode, if you want to display the value of a variable in the console window, you would use the following: println() _____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC
PAGE 7
OR print()
Using println() will add a newline at the end of the results, whereas p rint() will not.
This means, if you wanted to assign a variable and call it, it would look like this: var numberOfPeopleComingToParty = 10 print(numberOfPeopleComingToParty)
This would produce 10 on the display. Swift Variable Types What’s really great about Swift, is that Xcode is smart enough to know if we write var numberOfPeopleComingToParty = 10
it knows the object is a whole number (or integer), and if we write var numberOfPeopleComingToParty = “Hello World!”
it knows the object is a string of words (or String).
However, this is not always the case. If we needed to tell the program was the object is, we would need to write it like this: var numberOfPeopleComingToParty : String = “Hello World!”
This tells the program specifically that the object is a String. Swift is generally smart enough to figure a lot of this out, but it’s _____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC
PAGE 8
important that you know how to do the long hand because it will come into play later. There are four types: Bool Bool stands for boolean. A boolean value is either true or false. Think of a boolean value as describing a state that can be expressed with just two values. For example, a door is either open or closed, it is day or night, the light switch is on or off, etc. Bool’s are critical to computer programming because they play a critical role in helping the computer make decisions and execute different paths of a software program based on the bool value. Here is how a bool is declared in Swift. var todayIsSunny : Bool = true
Int Int represents an integer number between -2,147,483,648 and 2,147,483,647 . Integers do n ot contain decimal values or fractions. They are always positive or negative whole numbers including 0. Here is how an integer is declared in Swift: let numberOfFacesOnADodecahedron : Int = 12
Float A float represents a floating point number between 1.175494e-38 a nd 3.402823e+38. A Float can have a fractional component (decimal). It is declared as follows:
_____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC
PAGE 9
let earthsMeanGravitationalConstant : Float = 9.80665
Double A Double also represents a floating point number. A Double can also have a fractional component (decimal) and is more precise than the Float type(has more decimal places). In cases where Float or Double would work, Double is recommended by Apple and is the default floating-point number type used when using t ype inference. It is declared as follows: let earthsMeanGravitationalConstant : Double = 9.80665
String - String is a variable which holds a list of characters to form a string. Strings are always contained between two quotes. You can declare a string in the following way: var myFavoriteCity : String = ”Chicago, IL”
Camel Casing Names in Swift If you haven’t already noticed, the names of the variables and constants have these funny upper and lowercase characters going on. _____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC
PAGE 10
When using longer names, it is often the case that a single name is made up of multiple words. The tradition is to use camelCasing to make the variable easier to read. It allows us to be much more descriptive in what our assignment is. For example, welcomeMessage is much easier to decipher what that is for than wMss. Clarity is key when writing code because often times you are not the only one looking at your code. Clear description will help lead you to clean and transferable code. For example, if we wrote: numberofstatesintheus as our variable name, it would be descriptive but somewhat hard to read. numberOfStatesInTheUS is easier to read because we can visually see where the word boundaries are. The rules for camelCase are as follows: 1. Start every variable name with a lowercase character, unless it is a known abbreviation, in which case you can use the all caps version of the abbreviation. 2. Capitalize the first character of each n ew word . Note that other languages have a different standard. Ruby, for example, would want the variable name written as follows: number_of_states_in_the_US . They put an “underbar” between each word. The compiler (Xcode) doesn’t care how we name our variables and doesn’t enforce any standard.
_____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC