As you can see I dragged it and will add above the viewDidLoad and under the ViewController.swift when I’ll release the mouse a dialogue box will open

The connection is how you want your tag to perform in VC(ViewController.swift) there are following ways it can perform:

Outlet (after dropping in VC IBOutlet is made )

Action (IBAction is made)

Outlet Collection (Didn’t know about it yet as I’m also learning)

IB stands for Interface Builder which makes it simple for designing user interface without writing any code. Simple Drag and Drop is used.

IBOutlet is just to get the text from the Textfield and in Label it is used to change text or colour of the label or make it Hidden or Visible as per the use.

IBAction is an important one as it’s basically used on buttons and tells you what action or command you want to perform when it is pressed. It makes a method with a name which you gave in dialogue box.

Here on line 10 you can see guessInput.text! the “!” is used to force unwrap as the TextField may or may not contain the value. So swift returns the value in optional to handle the nil exception. The concept of optionals doesn’t exist in C or Objective-C. The nearest thing in Objective-C is the ability to return nil from a method that would otherwise return an object, with nil meaning “the absence of a valid object.”

Here the force unwrapping is used which means we are sure that guessInput will definitely contain a value, but what if user presses the button without entering any number. To handle this we should use Optional Binding Concept as below (replacing the line 10)

if let input = Int(guessInput.text!) { //Optional Binding

if input == randomNumber{

outputLabel.text = "Yeah!! You Got it Right"

} else {

outputLabel.text = "Ooh!! You Failed. It was \(randomNumber)"

}

} else {

outputLabel.text = "Please enter a number"

}

This means if guessInput is nil the outputLabel will show “Please enter a number”.

You can clone this from github.

If you liked this article, click the 💚 below.

In my next blog I’ll tell more concepts about Type Casting, Timers, Permanent Storage and more.