Rishav Pandey
Dec 26, 2016 · 4 min read
Swift 3 with Xcode 8

Introduction

Its been 20 days since I started learning iOS app development. I’m still in progress in developing my skills to have a grip for making some cool apps. I decided to share my knowledge about things that I learnt so far and the problems that I faced yet.

Downloading Xcode 8

The very initial step to get started is to get Xcode 8 with swift 3 which apple provides over here. You have to create an apple id (as most of the people with Macbook has).

Its worth to highlight over here that the iOS App development is purely for Mac computers as you can refer to apple

But no worries for other OS users here is the way which may help you to be passionate for iPhone app development.


So, let’s start with small app development to get you comfortable with Xcode environment.

How Many Fingers

This app is all about to get friendly with if-else statement in swift 3 with interaction with textfields, labels and buttons.

Fingers upto 5 I’m holding upon.

To make Textfield, button and labels interactive we just have to drag and drop from MainStory.swift to ViewController.swift by pressing control + drag and putting it under ViewController.swift class and above viewDdiLoad() method (I’ll discuss about this later).

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.

Aviabird

Aviabird Technologies provides enterprise software consulting in Angular, Rails, Elixir, Golang, React, Flutter and other technologies.

Rishav Pandey

Written by

Aviabird

Aviabird

Aviabird Technologies provides enterprise software consulting in Angular, Rails, Elixir, Golang, React, Flutter and other technologies.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade