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.
Ex:
var sortedStrings = sorted(stringArray)
{
$0.uppercaseString < $1.uppercaseString
}
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.
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.
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:
***********************
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:
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"]
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
Ex:
var sortedStrings = sorted(stringArray)
{
$0.uppercaseString < $1.uppercaseString
}
- Designed for Safety
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
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
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