iOS For Beginner

Swift Language

Swift is a new programming language for iOS, OS X, and watchOS app development. Nonetheless, many parts of Swift will be familiar from your experience of developing in C and Objective ­C.

Swift provides its own versions of all fundamental C and Objective ­C types, including Int for integers, Double and Float for floating­point values, Bool for Boolean values, and String for textual data. Swift also provides powerful versions of the three primary collection types, Array, Set, and Dictionary.

Like C, Swift uses variables to store and refer to values by an identifying name. Swift also makes extensive use of variables whose values cannot be changed. These are known as constants, and are much more powerful than constants in C. Constants are used throughout Swift to make code safer and clearer in intent when you work with values that do not need to change.

Swift Language

Swift is a type­safe language, which means the language helps you to be clear about the types of values your code can work with. If part of your code expects a String, type safety prevents you from passing it an Int by mistake. Likewise, type safety prevents you from accidentally passing an optional String to a piece of code that expects a non optional String. Type safety helps you catch and fix errors as early as possible in the development process. And about variables, constants, optional, enum, Functions, operators and all we have discussed in chapter 4.

You can get more information about swift language in Swift ­Apple WWDC 2014. Now in this chapter we will explain the all basic syntax in swift with one example demo.Learning swift is quite easy when you have strong basic fundamentals.But we recommend for all new developers to start with objective c first and then swift. Now let see all basic concepts that we exclude in chapter 4.

Now the swift have important feature called “Playground”.That is nothing but you can get the output in compile time. For example, open code ­> and select first option “Get started with a playground” and select “ iOS Playground” then click next.

iOS Playground

iOS Playground

After that give the name and click next and save the project where you want to save. Now the playground window will look like below image:



Now copy and paste the below code and we will explain what is what. If you create a program for OS X then you should mention header as import cocoa. And if you create program for iOS then you should mention header as import UIKit. Now lets see an example:

import UIKit
var str = “Hello, tutorial Diary”

This is nothing but,

● import UIKit is header file.Here we creating program for iOS
● Var is an variable and we declare the string name as str and we mention the str value as “ Hello , tutorial Diary”
● print(str) is nothing but the printing statement.

So the output will be Hello, tutorial Diary.


Like in objective c , comments are also using in swift .That comments line will be ignored by the compiler. Like:

// Example comment code

/* Example comment code */

WhiteSpace :

Whitespace is the term used in Swift to describe blanks, tabs, newline characters, and comments. White Spaces separate one part of a statement from another and enable the compiler to identify where one element in a int, ends .

int num = num1 + num2

Type Annotations:

A type annotation explicitly specifies the type of a variable or expression. Type annotations begin with a colon (:) and end with a type, as the following examples show:

func somename(a: Int) { }

String Literals:

Here we have some of the string literals which we are using inside our code.

\0Null Character
\fform feed
\nNew Line
\tHorizontal tab
\vVertical tab
\’single quote
\””Double Quote

Example : let str = “Hello\World\n\Hello\’Swift\'”

String Interpolation:

String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal. Each item that you insert into the string literal is wrapped in a pair of parentheses, prefixed by a backslash.For example:

let addition = 3
let total = “\(addition) times 2.5 is \(Double(addition) * 2.5)”

// total is “3 times 2.5 is 7.5”

String Concatenation:

We will explain with some code.So that you can understant how it works. Now,

let A = “ Spike”

Let B = “SS”

Let first = A +”, “ + B

Let second = “\(A), \(B)”

Likewise you can do like this also :

Var c = “spike”

c += “, SS”

let’s creates a constant. (sort of like an NSString). You can’t change its value once you have set it. You can still add it to other things and create new variables though.

var creates a variable. (sort of like NSMutableString) so you can change the value of it.

String Length:

The best example to get the string length is given below. We create a constant as assign the value for that,Now in print statement we have to mention the string name with character of that string name with synat .count, .endIndex

Let str: string = “ Hello, Spike”

print(str.characters.count) // o/p : 12

print(str.endIndex) // o/p : 12

We can declare two type of common syntax for string length.


An array stores values of the same type in an ordered list. The same value can appear in an array multiple times at different positions.The type of a Swift array is written in full as Array, where Element is the type of values the array is allowed to store. You can also write the type of an array in shorthand form as [Element]. Although the two forms are functionally identical, the shorthand form is preferred and is used throughout this guide when referring to the type of an array

For example :

var someInts = [Int]()

print(“someInts is of type [Int] with \(someInts.count) items.”)

// prints “someInts is of type [Int] with 0 items.”

Swift Closure :

Closures are self­contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective­C and to lambdas in other programming languages.

Closures can capture and store references to any constants and variables from the context in which they are defined. This is known as closing over those constants and variables, hence the name “closures”. Swift handles all of the memory management of capturing for you.

Closure syntax :

{ (parameters) ­> return type in statements


Objective C Fundamental (Prev Lesson)
(Next Lesson) Swift 2.0
Back to iOS For Beginner
Inquire Now
close slider