Chapter 4

Explaining the code and logic

We are using Object-Oriented Programming while making an app using swift. Now, I will explain how all the code and the logic works.

Object-oriented programming

If you go to the Main.storyboard file, you can find the connection between the button and the method. If you go to the last option on the right pane (connection inspector), you can find the list of sent events for the button.

As you can see, we connected the showMessage method to the Touch Up Inside event when we dragged a line from the button to the view controller option.

The code

Now, first things first, we created a class called ViewController which was auto-created by Xcode when we selected the app template. Then, automatically, the ViewController class was linked to the View Controller in the interface builder. You can check this by clicking on the View Controller Scene and going to the Identity Inspector option. You can see that the custom class connected is ViewController.

Coming back to the code, you can see that we used a semi-colon and then wrote UIViewController. What is this? iOS SDK has some predefined classes that we can use and UIViewController is one of them. Here, we are extending that class to add our own custom functions, else the class just outputs an empty screen. In order to use the APIs (methods and classes), we need to import the corresponding framework from iOS SDK. The framework we need is UIKit and you can see that we imported it in the first line. This framework has the APIs like UIAlertController and UIViewController and allows us to use variable types like UIButton.

Declaring the method

In order to declare the method, we first use the @IBAction keyword which indicates that you are connecting your source code to the interface and it shows that you are the method that can be exposed to the Interface Builder. This is why you can see the method after try to customize an event (like the Touch Up Inside event for the UIButton). We use the func keyword to declare a method in Swift and then write the name of the method. Then, we can write the parameters for the method. In this case, the parameter is sender and the type is UIButton. This parameter indicates the object in the Interface Builder that sends a message to the method.

Inside the method

Then, we write the code to tell the program what the method does. We create an emoji dictionary and then we assign a variable to the sender which is the button. Since we will link this to the event “Touch Up Inside”, this code will be used to execute the method when the button is touched and the finger is lifted. Then we use properties of the button to retrieve the text on the label (i.e. the emoji). Using a dot (.) after the variable name allows us to retrieve information like the label text. Then, we create a variable to get the meaning of the emoji from the dictionary.

Using the UIAlertController, we create an alert and we customize the title, message and the style of the alert. We then assign a constant to it and add an action for the ok button on the alert. The last line is just used to present the alert on the screen and animate it.

The method ViewDidLoad() is automatically called by the UIViewController class and we call an override function to customize the properties of the view controller on loading.

Other methods like ViewWillDissapear and ViewDidDissapear are called when the app is closed to go to the home. We can override and then customize these functions too.

Run Button

Compiling: Xcode converts your code from Swift to Machine code so that iOS can understand it.

Packaging: Resources like text, images, videos, etc. are packaged when the project is built.

Running: Actually displays the app.

That is it for this chapter. Next up, we shall discuss Auto Layout.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store