Vars, vars, all types of vars

Introduction to Swift · Lesson 5 · 14 minutes


What's a var? What sorts of vars do we see in Swift?

All of the code that we write in this section and the others is open source and available here in its entirety: https://github.com/onemonth/one-month-ios

For additional information on vars, lets and optionals check out Optionals for Beginners.

Play in your first playground

MyPlayground.playground

//: Playground - noun: a place where people can play

import UIKit

var myString = "Hello, playground"

var myOtherString: String
myOtherString = "something cool"

var myInt = 1000
myInt = myInt * 5

var myOtherInt: Int
myOtherInt = 999

var myFloat = 0.5
var myOtherFloat = 0.9999
myOtherFloat = 1.02

var myArray = ["something"]

var myOtherArray: Array<String>
myOtherArray = ["zero"]
myOtherArray.append("one")
myOtherArray.append("two")

var myDictionary = ["key" : "value"]

var myOtherDictionary: Dictionary<String, String>
myOtherDictionary = ["one" : "value1"]
myOtherDictionary["two"] = "value2"

myOtherDictionary

5 Questions

Having trouble? Don't worry, you're probably not the only one. Check out the questions other students have or ask yours.