Thursday, July 3, 2014

Assertions in Swift Langauge

Optionals enable you to check for values that may or may not exist, and to write code that copes gracefully with the absence of a value. In some cases, however, it is simply not possible for your code to continue execution if a value does not exist, or if a provided value does not satisfy certain conditions. In these situations, you can trigger an assertion in your code to end code execution and to provide an opportunity to debug the cause of the absent or invalid value.

Debugging with Assertions

An assertion is a runtime check that a logical condition definitely evaluates to true. Literally put, an assertion “asserts” that a condition is true. You use an assertion to make sure that an essential condition is satisfied before executing any further code. If the condition evaluates to true, code execution continues as usual; if the condition evaluates to false, code execution ends, and your app is terminated. If your code triggers an assertion while running in a debug environment, such as when
you build and run an app in Xcode, you can see exactly where the invalid state occurred and query the state of your app at the time that the assertion was triggered. An assertion also lets you provide a suitable debug message as to the nature of the assert. You write an assertion by calling the global assert function. You pass the assert function an expression that evaluates to true or false and a message that should be displayed if the result of the condition is false:

let age = -3
assert(age >= 0, "A person's age cannot be less than zero")
// this causes the assertion to trigger, because age is not >= 0

In this example, code execution will continue only if age >= 0 evaluates to true, that is, if the value of age is non-negative. If the value of age is negative, as in the code above, then age >= 0 evaluates to false, and the assertion is triggered, terminating the application. Assertion messages cannot use string interpolation. The assertion message can be omitted if desired, as in the following example:

assert(age >= 0)

When to Use Assertions

Use an assertion whenever a condition has the potential to be false, but must definitely be true in order for your code to continue execution. Suitable scenarios for an assertion check include:

NOTE

 Assertions cause your app to terminate and are not a substitute for designing your code in such a way that An integer subscript index is passed to a custom subscript implementation, but the subscript index value could be too low or too high. A value is passed to a function, but an invalid value means that the function cannot fulfill its task. An optional value is currently nil, but a non-nil value is essential for subsequent code to execute successfully.
invalid conditions are unlikely to arise. Nonetheless, in situations where invalid conditions are possible, an assertion is an effective way to ensure that such conditions are highlighted and noticed during development, before your app is published.

Wednesday, July 2, 2014

Tuples in Swift Language

Tuples group multiple values into a single compound value. The values within a tuple can be of any type and do not have to be of the same type as each other. In this example, (12, "student Name") is a tuple that describes an student data.

let studentdata = (12, "John")

The (12, "John") tuple groups together an Int and a String to give the student data two separate values: a number and a name of student. It can be described as “a tuple of type (Int, String)”.

You can create tuples from any permutation of types, and they can contain as many different types as you like. There’s nothing stopping you from having a tuple of type (Int, Int, Int), or (String, Bool), or indeed any other permutation you require. You can decompose a tuple’s contents into separate constants or variables, which you then access as usual:

let (rollnumber, studentname) = studentdata
println("The roll number is \(rollnumber)")
// prints "The roll number is 12"

println("The student name is \(studentname)")
// prints "The student name is John"

Alternatively, access the individual element values in a tuple using index numbers starting at zero:
println("The roll number is \(studentdata.0)")
// prints "The roll number is 12"
println("The student name is \(studentdata.1)")
// prints "The student name is John"

You can name the individual elements in a tuple when the tuple is defined:
let studentinfo = (rollNo: 20, Name: "Howdy")
If you name the elements in a tuple, you can use the element names to access the values of those elements:

println("The rollNo is \(studentinfo.rollNo)")
// prints "The rollNo  is 20"

println("The Name is \(studentinfo.Name)")
// prints "The Name is Howdy"

Tuples are particularly useful as the return values of functions. A function that tries to retrieve a web page might return the (Int, String) tuple type to describe the success or failure of the page retrieval. By returning a tuple with two distinct values, each of a different type, the function provides more useful information about its outcome than if it could only return a single value of a single type.

Basic operator in Swift Langauge

Assignment Operator

The assignment operator (a = b) initializes or updates the value of a with the value of b:

let b = 10
var a = 5
a = b
// a is now equal to 10

If the right side of the assignment is a tuple with multiple values, its elements can be decomposed into multiple constants or variables at once:

let (x, y) = (1, 2)
// x is equal to 1, and y is equal to 2

Unlike the assignment operator in C and Objective-C, the assignment operator in Swift does not itself return a value. The following statement is not valid:

if x = y {
    // this is not valid, because x = y does not return a value
}
This feature prevents the assignment operator (=) from being used by accident when the equal to operator (==) is actually intended. By making if x = y invalid, Swift helps you to avoid these kinds of errors in your code.

Arithmetic Operators


Swift supports the four standard arithmetic operators for all number types:
Addition (+) Subtraction (-) Multiplication (*) Division (/)

1 + 2       // equals 3
5 - 3       // equals 2
2 * 3       // equals 6
10.0 / 2.5  // equals 4.0

Unlike the arithmetic operators in C and Objective-C, the Swift arithmetic operators do not allow values to overflow by default. You can opt in to value overflow behavior by using Swift’s overflow operators (such as a &+ b). See Overflow Operators. The addition operator is also supported for String concatenation:
"hello, " + "world"  // equals "hello, world"

Two Character values, or one Character value and one String value, can be added together to make a new String value:

let firstname: Character = "John"
let lastname: Character = "Howdy"
let name = firstname+ lastname

// name is equal to "JohnHowdy"

Introduction to Swift Programming Langauge

Welcome to Swift Programming Langauge


Swift is a new programming language for iOS and OS X apps that builds on the best of C and Objective-C, without the constraints of C compatibility. Swift adopts safe programming patterns and adds modern features to make programming easier, more flexible, and more fun. Swift’s clean slate, backed by the mature and much-loved Cocoa and Cocoa Touch frameworks, is an opportunity to reimagine how software development works.

Swift feels familiar to Objective-C developers. It adopts the readability of Objective-C’s named parameters and the power of Objective-C’s dynamic object model. It provides seamless access to existing Cocoa frameworks and mix-and-match interoperability with Objective-C code. Building from this common ground, Swift introduces many new features and unifies the procedural and object-oriented portions of the language.

Basic Values in swift Programming Language

Tradition suggests that the first program in a new language should print the words “Hello, world” on the screen. In Swift, this can be done in a single line: