As we are studying the Swift programming language for past couple of months. While studying the language I cam across many features of the programming. So I thought to compile all those features with some brief introduction so that if some one just want to have a sneak peak at the features he/she can just go though this blog.
So this is the part -1 of the series I am thinking to write. Hope to complete it soon and cover the features in its entirety.
So this is the part -1 of the series I am thinking to write. Hope to complete it soon and cover the features in its entirety.
Objective C has been the core programming language for Apple
software Development. It has been there for 20 years and serving us well. As
per Apple they have worked for 4 years to design Swift Programming Language.
So what is it that they wanted to achieve with Swift? As per
Apple during the Swift Programming language intro in WWDC 2014 they want “Objective
C without the baggage of C”.
Swift is Fast, Safe, Modern and Interactive programming
language. Here are the few key points of Swift programming language:
• Adapts
safe programming patterns by using the Type Inference and Generics.
• It
adopts the readability of Objective-C’s named parameters and the power of
Objective-C’s dynamic Object Model.
• Provides
seamless access to the existing Cocoa Framework and Objective-c code.
• Develop apps for iOS and Mac OS both.
You might have heard about the playgrounds the new feature
in XCode 6 where you can use Swift language and see the output of the
programming as you finish typing the code. We can discuss Playground and its
features in other blog. Today we will talk about what all features Swift
Programming language has come up with and will have brief introduction about
each feature for you to kick start the language.
Simple Types
var and let
Use var to declare a variable and let to declare a
constant. The value of constant doesn’t
need to be known at the compile time but you must assign it a value exactly
once.
Control Flow
Swift supports all the familiar control flow statements like
- For
- For in
- While
- Do-While
to perform the task multiple times and conditional
statements
- Switch
- If -else
Statements for executing the difference branches of the code
depending upon the condition.
So what’s new in these statements that swift has come with?
Fallthrough
This is new in Switch case statement. Unline c and Objective
C switch case statements do not fall through to the next case statement if you are
missing the break statement. Instead it finishes the execution of the block of
the first matched case without the break statement. Break statement in the
swift switch case is optional.
The body of each case must contain at
least one executable statement. It is not valid to write the following code,
because the first case is empty:
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a":
case "A":
println("The letter A")
default:
println("Not the letter A")
}
// this will report a compile-time error
If you really need to fallthrough a case by case than you
can do that by writing fallthrough keyword.
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += " a prime number, and
also"
fallthrough
default:
description += " an integer."
}
println(description)
// prints "The number 5 is a prime number, and also
an integer."
Switches support any type of data and variety of comparisons.
Functions and Closures
Every function in Swift has a type, consisting of the
function’s parameter types and return type. You can use this type like any other
type in Swift, which makes it easy to pass functions as parameters to other
functions, and to return functions from functions. Functions can also be
written within other functions to encapsulate useful functionality within a
nested function scope.
Functions are actually special cases of closures. Closure
can be written using {} without specifying any name to it. Closures in Swift
are similar to blocks in C and Objective-C and to lambdas in other programming
languages. Reading lambda for C# is encouraging as that what is used mostly
these days. J
Use func to declare a
function. Call a function by following its name with a list of arguments in
parentheses. Use -> to separate the parameter names and types from the
function’s return type.
func greatDay(name: String, day: String) -> String {
return "Hey
\(name) today is: \(day)"
}
greatDay("karm", "sunday")
So what is it that func in Swift provide?
Functions can return multiple values
Functions in swift can return more than one values.
External Parameter Names
You can specify an external name for the parameter,
which means you can specify a different name for named parameter for using it
as local variable within the function and different name while calling the
function.
Shorthand External Parameter Names
If you want to specify the same name as local and
external than you don’t need to write the same name just prefix the name with #
and you are good to go.
Variadic Parameters
This means you can specify whether your function is expecting Zero or more an zero elements. The values passed to a variadic parameter are
made available within the function’s body as an array of the appropriate type.
Constant and Variable Parameters
By default the parameters specified in the function declaration are constant. If you try to change the value of the parameter in the function body you will receive a compile time error. If you want to change the value of the parameter you need to prefix the parameter declaration with var keyword.In-Out Parameters
Variable parameters, as described above, can only be changed within the function itself. If you want a function to modify a parameter’s value, and you want those changes to persist after the function call has ended, define that parameter as an in-out parameter instead. You write an in-out parameter by placing the inout keyword at the start of its parameter definition. An in-out parameter has a value that is passed in to the function, is modified by the function, and is passed back out of the function to replace the original value.
So these were features in the functions.
We will start with Closures in the next part.
Keep reading and Happy Coding!!!
No comments:
Post a Comment