Swift Data Types

There are two fundamental data types in Swift

Value Types 
  • Int, Floating-Points, Booleans, Characters, Strings , Arrays, Dictionaries and Tuples (which are implemented as Structures)
  • Optionals (which are implemented as Enumerations)
Reference Types
  • Classes
  • Functions and Closures 

Optionals in Swift


var a : Int            // create a swift variable of type int
var b = a + 10      //compiler will thrown an error because a is not initialized

Alternative to above

When your using ‘?”/optional , that means your explicitly telling the compiler that a may or may not have the value.

var a : Int?
var b = a! + 5     // the compiler suggest to unwrap the optional, this is called force unwrapping and program will crash if in case a does not have value so force unwrapping of optionals is always dangerous and should not do it unless your sure about var holding a value.

then what is the safe way to deal with optionals? answer is optional binding 

if let nonOptionalValue = a {
var b = nonOptionalValue + 5  // if a is nil, this statement is not executed and force unwrapping is not required in this case because we knew that a is not nil and has a value for sure.

}

Pointers

In Objective C all objects are referenced using pointers:

A pointer is a variable which stores memory address instead of value itself.

how big is int, 4 bytes
how big is char, 1 byte
how big is String, image or video?  we often don’t know and it might even change during our program so we need more flexible way to manage objects and pointers are one way of allowing us to do that.

Using pointers allows us to pass around the objects (just by passing around the address of object) without having need to copy them which is time consuming and inefficient  

E.g.   NSString* welcomeString;
         NSDate* todayDate;

welcomeString = @“Welcome to Objective C”;

Here the string 'Welcome to Objective C’ string is not directly stored in ‘welcomeString’ instead the address/memory location of 'Welcome to Objective C' is stored in ‘welcomeString which is contrary to 'int highScore = 100’ where ‘highScore’ variable directly stores value. ( this is because objects are more complex than primitive types)

if you want to make and use of simple object, we need pointers to reference them.
Note: Explicit use of pointers in Swift is not required unlike Objective C. e.g let someInstance = SomeClass ( ) whereas in objective C it is SomeClass *someInstance = [SomeClass alloc] init];

Structures in Objective C and Swift

Structure is a complex data type which contains individual elements which defer in type. 
There are many situations in programming where discrete types have to be identified together as one unit. In this situations we make use of Struct.

Structs are used to represent a record, Suppose you want to keep track of your books in a Library. you might want to track the following attributes about each book.

Struct Book {
Title
Author 
Subject
Book ID
};

Struck Person {
Name
Age
Height
Weight
Gender
Color
};

Individual elements of Struct are referred to as members

Unlike Objective C, classes in Swift, structures can have methods, properties, initializers, and conform to protocols. The main difference between classes and structures is that classes are passed by reference, while structs are passed by value.

Set vs Array

The main difference is that NSArray is an ordered collection and NSSet is an unordered list of unique elements.

Set
  • Primarily access items by comparison   
E.g    let ingredients = [“Tea Powder”, “Sugar”, “Hot Water”, “Milk”, "Cardamom"]
          if ingredients.contains(“Cardamom”) {
print(“It is Cardamom Tea”)
}
  • unordered
  • does not allow duplicates
Array

  • Can access items by Index
  • Ordered
  • Allow duplicates

Dynamic Typing & Dynamic Binding:

Dynamic Typing in Objective C means that the class of an object type id is unknown at the compile time and is discovered at the run time based on the message sent to the Object.

Dynamic typing allows us to declare a variable that is capable of storing any type of object regardless of its class origin, this is achieved using the Objective C id type, the id type is a special, general purpose data type that can be assigned an object of any type.

Dynamic binding takes this one step further by allowing methods on object type id to be called without prior knowledge of the type of an object currently assigned to the variable.


Different states of an iOS app

Different states of an iOS app:
  1. Not Running 
  2. Inactive
  3. active
  4. Background
  5. Suspended
1. Not Running

App is usually in this state when it is not launched yet or was running but was terminated by the system 

2. Inactive

App is running in the foreground but is currently not receiving any user events (it may be executing other code though), App usually enter into this state only briefly as it transitions into different state.

3. Active

App is in the Foreground and receiving user events. This is normal mode for foreground apps.

4. Background

App is in the background and executing code. Most app enters into this state briefly on their way to being suspended. However the app that request the extra execution time may remain in this state for a period of time.

5. Suspended

App is in the background but not executing code. The system moves apps to this state automatically and does not notify them before doing so, while suspended app remains in memory but does not execute any code

When a low memory condition occurs, the system may purge suspended apps without notice to make more space for foreground app.

Apps must be prepared for termination to happen any time and should not wait to save user data to perform other critical tasks. Suspended apps receives no notification when they are terminated.


if the app is currently running in the background and not suspended, the system calls  applicationWillTerminate: of its app delegate prior to termination. The system does not call this method when the device reboots.