Absolute beginner iOS tutorial: Age Calculator application in Swift

An absolute beginner’s guide, a step by step Swift tutorial, we will learn how to use Date Picker, UIAlertController and Date objects

In this Step-by-Step tutorial we will create a simple age calculator application.

The user select his birth date from a date picker then he press a button,we then calculate his current age in years, months and days

Finally we display it in a UILabel.

What we will learn

How to calculate difference between two Date objects

How to use Calendar to get Date Components

How to use UIDatePicker

How to add Auto layout constraints to simple view

How to create and display a UIAlertController Continue reading “Absolute beginner iOS tutorial: Age Calculator application in Swift”

How to create a Computed property in Swift

What is a computed property

Classes, Structures and Enums may define a computed property.

It is a property that does not store value, instead it defines getter and setter to retrieve and set other properties and values indirectly. Continue reading “How to create a Computed property in Swift”

What is the difference between Any and AnyObject in Swift

What are Any and AnyObject



Any and AnyObject are special types for working with nonspecific types.

Using Any and AnyObject is not recommended and it is always better to be specific about the types you work with. Continue reading “What is the difference between Any and AnyObject in Swift”

Downcasting using as! and as? in Swift

How to use as! and as? for downcasting and what are the differences between them

What is Downcasting ?



Behind the scenes a variable or constant of a class type may be a subclass of another class type.

For example a variable of type Any may be referring to a String. Continue reading “Downcasting using as! and as? in Swift”

Error handling using throws and do-catch in Swift

Example of using throws and do-catch and try to handle errors in Swift

Throwing functions in Swift



To indicate that a function can throw error you just add the word “throws” after its parameters and before its return type.

It is then called a throwing function. Continue reading “Error handling using throws and do-catch in Swift”

How to save Array of custom objects to NSUserDefaults

Using NSCoding, NSKeyedArchiver and NSKeyedUnarchiver to save array of custom objects to NSUserDefaults

NSCoding protocol



NSCoding protocol enables an object to be encoded or decoded for archiving and saving to disk.

It declares two methods that your object must implement in order to be encoded and decoded. Continue reading “How to save Array of custom objects to NSUserDefaults”

What exactly are Generics

In fact Array and Dictionary are generic collections, this is why you can create an Array of Int , String or any other type

What are Generics



Generics are one of the most powerful features of Swift, It allows you to create flexible Functions and Types that can work with any type.

If you used Array or Dictionary before in Swift then you already knows about Generics.
In fact Array and Dictionary are generic collections, this is why you can create an Array of Int , String or any other type and the same with Dictionary. Continue reading “What exactly are Generics”