So today I covered a lot of different aspects ranging from Variables, arrays, constants to motion gestures. Below I am typing a few things I thought were most important.

  • While connecting a label/image from the Storyboard to the code editor we see the word “IBoutlet”, which means Interface Builder outlet.
  • Connection type for buttons is both action and outlet , whereas in case of labels and images it is only outlet. (As buttons can be used to obtain user input and perform an action afterwards.)

Declaring a variable:

Declaring a constant:

let fixedAmount : Int = 10 

The difference between declaring a normal variable and using “let” to declare a constant is that we can change the value of the normal variable even after declaring it in subsequent lines of code, but we cannot change the value of a constant. It is fixed.

  • If we change the variable name after we have connected it in our code, then we also need to remove the connection and reconnect it to our code.

Declaring a string:

var myName : String = ‘Prateek’


var myArray = [‘itemone’, ‘itemtwo’, ‘itemthree’]

Generating random number:


  • In the above example I have wrapped the arc4random_uniform() into Int() so that Xcode treats it as in integer. Also, it will generate a random number upto 5.

Function – Declaring:

func myNewFunction() { your code goes here }

Function – Calling: 


Changing Image element using code:

myImageElement.image = UIImage(named: filename)

  • In the above snipped, myImageElement is the name of your variable and filename refers to the name of the image file.

Creating a dice rolling app:

  1. Added the Buttons, labels and images to the storyboard.
  2. Linked them to the View Controller file.
  3. Declared a variable holding an int value of 0
  4. Declaring an array filled with the file names of the images.
  5. Set the variable declared in step 3 to generate a random number.
  6. Used the code mentioned above to change the image element to the array of file names selected randomly using the variable in step 5.
  7. Step 5 & 6 were executed in the button function code.

Shake Gesture:

  • Type motionEnded and hit enter, and Xcode will automatically write the full code. Now place the code you want to execute on shake gesture inside the curly brackets.