iOS For Beginner

Basics of Swift & Objective C

The very first thing to keep in mind is that, for creating any type of iOS app we must be strong in basic syntax of programming. In this chapter we explain the basic syntax and the main difference between two languages (objective c & swift). Let’s first see some important concepts involved in swift.

Variable & Constants:

Declaring a variable in swift is done using var Keyword

var A = 99
var B = “Hello World”

You notice that two variable A and B are of different types. A is an integer ,while B is a string. Swift is a type of safe language and it can reduce variable type from the assigned value. To make it more readable, you can write something like this :

var y:Int y = 3

Constants are also declared in similar way , but we declare them using let instead of var. The value of a constant doesn’t need to be known in compile time.

let c = 12

Optional :

Constants need to be initialized when we declare them, and variables need to be initialized before use them. Swift introduces optional value, optional value can have a value or be nil. If you take a look at the following code. In that you will notice that x was assigned an optional value “some name”. This means that swift compiler was aware that x might also be nil

var x : string? = “some name”

Functions:

Function definition in swift is different from C. A simple function definition is below:

func some_Function(s:String, i:Int) ­ > Bool
{
// Your code
}

Swift functions are first class types. This means that you can assign functions to variables, pass them as parameters to other functions, or make them return types:

func string_Length(s:String) > Int
{
}

func string_Value(s:String) > Int { if let x = s.toInt()
return countElements(s)
{
return x
}
return 0
}

func doSomething(f:String > Int, s:String) > Int
{
}

let f1 = stringLength
let f2 = stringValue

Functions can also return other functions:

func compare_GreaterThan(a: Int, b: Int) > Bool
{
return a > b
}

func compare_LessThan(a: Int, b: Int) > Bool
{
return a < b }

Enumerations:

Enumerations in Swift are much more powerful than in Objective C. As Swift struts, they can have methods, and are passed by value:

enum Mobile Device : String
{
case iPhone = “iPhone”, Android = “Android”, WP8 = “Windows iPhone8”, BB = “BlackBerry” func name() > String
{
return self.toRaw()
}
}

Now let’s have a look on the syntax difference between two languages. Objective C is often seen as a barrier to beginners because of its syntax. For example, to initialize an array in.

Objective c :

NSMutableArray *array = [[NSMutableArray alloc] init];

But in swift :

var array = [Int]()

One more example :

NSString *string Name = @”Tutorial Diary”;

NSLog(@”%@”, string Name);

NSLog() ­ is nothing but the print statement of “stringName”

But in swift :

let string Name = “Tutorial Diary”
println(“\(stringname)”)

Basic Operators :

The if statement with comparison operator are often used in conditional statement:

let name = “ james” if name == “james”
{
println (“hello james” ) }else {
println (“oops check the name”)
}

For loop :

In addition to for in loop , swift support c style for loop with a condition and increment:

for var index = 0; index < 3; ++index { println (“index is \(index)”) }

Here is the general form of this loop format :

for initialization ; condition; increment {
statement
}

In advance we have also bitwise AND,OR,NOR ,.etc operator. So these are some important thing to know in basic swift language. Now let see the difference between both languages. Even for your interview it will be very useful

SWIFT OBJECTIVE ­ C
Swift brings type safety to iOS development and because of the static
type can optimize call and directly call the methods.
Dynamic typing is support means type of the object it points is not checked at compile time.
Swift strings are improvement over Objective C without worrying about using a mutable or immutable string type. By using NSString objective C string is represented and it’s a subclass of NSMutableString which provide many different ways to creating objects.
File extension .swift File extension .h,.m
swift is friendly to new programmers which support playgrounds an innovative feature which allow programmer to experiment with the code and see results immediately without run the app. objective C is beginning of iOS and their syntax is easy to understand features that makes it user friendly which also hold much
importance and quite popular.

Some of the advantages of swift over Objective C is that memory is managed automatically, map and filter functional programming is a huge advantage, healthy debugging console in Xcode which include an interactive version of swift.

Please login to get access to the quiz
Model View Controller coding pattern (Prev Lesson)
(Next Lesson) Objective C Fundamental
Back to iOS For Beginner
Inquire Now
close slider