Friday 19 December 2014

Swift Programming Basics Part-1

Swift is an innovative new programming language for Cocoa and Cocoa Touch released at WWDC. 
Swift code works side-by-side with Objective-C.
When talking about Swift, Apple refers to three key considerations: Safe, Modern and Powerful.

Features of Swift:
  • Modern
Swift is the result of the latest research on programming languages, combined with decades of experience building Apple platforms. Named parameters brought forward from Objective-C are expressed in a clean syntax that makes APIs in Swift even easier to read and maintain.
Ex:
var sortedStrings = sorted(stringArray)
 {
$0.uppercaseString < $1.uppercaseString
 }
  • Designed for Safety
Swift eliminates entire classes of unsafe code. Variables are always initialized before use, arrays and integers are checked for overflow, and memory is managed automatically. Syntax is tuned to make it easy to define your intent — for example, simple three-character keywords define a variable (var) or constant (let).

The safe patterns in Swift are tuned for the powerful Cocoa and Cocoa Touch APIs. Understanding and properly handling cases where objects are nil is fundamental to the frameworks, and Swift code makes this extremely easy. Adding a single character can replace what used to be an entire line of code in Objective-C. This all works together to make building iOS and Mac apps easier and safer than ever before.

  • Fast and Powerful
From its earliest conception, Swift was built to be fast. Using the incredibly high-performance LLVM compiler, Swift code is transformed into optimized native code, tuned to get the most out of modern Mac, iPhone, and iPad hardware. The syntax and standard library have also been tuned to make the most obvious way to write your code also perform the best.

Swift is a successor to the C and Objective-C languages. It includes low-level primitives such as types, flow control, and operators. It also provides object-oriented features such as classes, protocols, and generics, giving Cocoa and Cocoa Touch developers the performance and power they demand.

  • Interactive Playgrounds
A playground is a new type of file that allows you to test out Swift code, and see the results of each line in the sidebar. For example, add the following lines to your playground:
let tutorialTeam = 60
let editorialTeam = 17
let totalTeam = tutorialTeam + editorialTeam
Playgrounds make writing Swift code incredibly simple and fun. Type a line of code and the result appears immediately. If your code runs over time, for instance through a loop, you can watch its progress in the timeline assistant. The timeline displays variables in a graph, draws each step when composing a view, and can play an animated SpriteKit scene. When you’ve perfected your code in the playground, simply move that code into your project.


Prerequisites for starting Swift programming:

  • A mac running OSX Mavericks or Yosemite
  • A copy of Xcode 6

            ***********************

Declaring variables in Swift:

Variables allow you to store data.
To declare a variable you have to use the keyword "var"

Ex:

var myString: String = "Hello World" 
//This line of code instructs the system that you want to create a variable named myString which is of type String and it will contain the text, “Hello World”.
                          (or)
var myString = "Hello World"
//Swift is smart enough to infer that if you are assigning a string to a variable and in fact that variable will be of type string. So you need not explicitly specify the type as in the above example

Variables can be modified once created so we could add another line and change "Hello World" to something else

myString = "Hello"

Other Exps on Variables:

var sal: Double = 70000.0
or
var sal = 70000.0  //Double

Constants or  Immutable Variables:

To create an immutable variable or constant you need to use the keyword 'let'.
Ex:
let someConstant = 10

Constants cannot be modified once created

someConstant = 20 //Compiler error

Other Exs:


let myNum: Int = 204

or
let myNum = 204  // Int

let isReal: Bool = true

or
let isReal = true  //Bool


Collection Types:
Collection Types are of 2 types.
Arrays  and Dictionarys

Arrays :

Array is a collection of data items which can be accessed via an index beginning with 0.

Ex:

var nameArray = ["Teja", "Kumar", "Varma", "Raja"]
or
var nameArray: [String] = ["Jack", "Queen", "King"]

==>Modifing An Array::

Consider an array alphaNumbers
var alphaNumbers = ["One", "Two", "Three"]

-->To add another item to alphaNumbers we use the ‘+=’ operator.
alphaNumbers += "Four"

-->To add multiple items to alphaNumbers array we should simply append an array.
alphaNumbers += ["Five", "Six"]

-->To replace an existing item in the array simply subscript that item and provide a new value:
alphaNumbers [0] = "1n"

Dictionaries:

var colorsDictionary = ["PrimaryColor":"Green", "SecondaryColor":"Red"]

No comments:

Post a Comment