Wednesday, June 25, 2014

Optionals in Swift Programming Language

As we learned in my other blog about Initializers in Swift Programming Language that every variable in Swift should be initiazed before trying to access the value.

So what if you do not want to initialize a variable. That is where Optionals in Swift comes into play.

Before start discussing about Optionals you should know about nil. nil in Swift means nothing, no value at all.

Lets see what optionals provides us:

  • Optionals can store value nil which means no value.
  • Any non optional can not store nil.
  • you can check for options have nil or value by just writing if value no need to add extra code for == nil. because optionals can be used in boolean context.
Reading a value from the Optional is called unwrapping the Optional.


How to unwrap optionals?
  • using the force unwrap operator !.  but if you use this operator without checking for nil you may end up with an error at runtime.
  • using the if let statement you can check for the value and unwrap it once. This is called optional binding.

Optional Binding


Saturday, June 21, 2014

Initializers in Swift Programming Language

As the name says Initialization. Initializers are declared to set the initial values of the type when any object of type gets initialized. Initializers are declared using the the syntax

init() {…}

you can also pass parameters to the initializers. So As the Swift programming enforces one simple rule

“Every value must be initialized before it is used”

Only exception to the above rule is optionals where we default their vale to nil. 

So what the above rule means? Does it mean that I cannot write something like

var someString : String

No, It means before you access any value it must be initialized in every possible branch of the execution of the code. If you don’t do it you will get a compile time error. Initializers have the responsibility to fully initialize an instance.

Initializers in Structure

So if you have a structure like this

struct Person
{
    var firstName : String
    var LastName: String
    init(first: String, last: String)
    {
        firstName = first
        lastName = last
    }
}
  • Say you forgot to initialize the firstName in the initiazer, than compiler will generate a compile time error.
  • Also if you try to call any method before initializing all the variables in the struct, you will get a compile time error.
  • you can write initializers for Structures and Classes both.


Memberwise Initializer

What if you do not specify any Initializer to the struct or class? 

Swift Compiler provide you with a Memberwise initializer. So for the above structure if you do not have the initializer declared you can still initialize the values by saying

Tuesday, June 17, 2014

Swift Playgrounds Introduction

So finally I had some time to explore the swift playgrounds. One thing I would like to say about playgrounds is that It's awesome and cool...

So here is the icon that they have given to the playground document type.

Clearly icon goes with the name. But believe me it does goes with the icon, its a child play to use the playground. You just open up the playground and you are ready to code.

Saturday, June 7, 2014

Swift programming Tutorials - Functions and Closures

This is the part 1 of the series of the Tutorials of Swift programming. You can take a quick look at the Introduction to Swift.

In this Tutorials we will learn about

Functions

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.


Few Points of Functions:
  • Functions can return multiple values using Tuples
  • Functions can accept variable number of parameters and collect them into an array.
  • Nested functions
  • Functions can accept other functions as parameters.
  • Functions can have function as its return value. Because functions are first-class type.
  • Functions are special cases of closures

Thursday, June 5, 2014

Swift programming language Introduction

I was on my way to learn the all mighty Objective-C, and suddenly Apple has come up with the super cool programming language "Swift"....

We will discuss few things about Swift and you will see more Tutorials coming on the way here regarding this.

Here are few of the highlights of the Swift.

  • Adapts Safe Programming Patterns
  • Future of the Apple Software Development
  • Provides access to the existing Foundation and Cocoa Framework.
  • Support both iOS and Mac Apps development.
Best thing that comes with Swift is...

Playground

Playground is an innovative feature that lets you play with the programming language and you can see the results of the program on the fly without even building and running the code. What it does: