Tuesday 4 October 2016

Variables and Type conversion

Variables

When you know you will need to change some data, you have to use a variable.

var variableNumber: Int = 420
You have to declare constants using let and  variables using var.
Type Conversion 
Sometimes you will have data which need to be converted it to another format.
var first: Int = 1000 // variable first with int value 1000

var second: Double = 122.5 // variable second with value 122.5
first =  Int(second) // Type conversion of second to Integer

Constants

In this Blogpost, you'll learn constants.

You will learn, how to declare, name and change them. 

You'll also learn about type inference (Swift's most important features).

Constants

let firstConstant: Int = 100 

This declares a constant called 
firstConstant which is of type Int. 
Then it sets the value of the firstConstant to the number 100.

let  
piConstant: Double = 3.141

This is similar to the above constant, except the name and the type are different. 
This time the constant is a Double.

Once you've declared a constant, you can not change its data

Sunday 24 April 2016

Apple's New Swift Language - Basic info

  • Swift is a new language designed by Apple for developing iOS and OS X applications.
  • It takes the best parts of C and Objective-C and adapts them with modern features and patterns. Swift-compiled programs will run on iOS7 or newer and OS X 10.9 (Mavericks) or newer.
  • The two main goals for the language are compatibility with the Cocoa and Cocoa Touch frameworks and safety, as you’ll see in the upcoming chapters. If you’ve been using Objective-C, especially the modern syntax, Swift will feel familiar.
  • However, Swift’s syntax is actually a major departure from Objective-C. It takes lots of cues from programming languages such as Haskell, C#, Ruby, and Python.
    Improvements over Objective-C

    Type Inference 
  • you can figure out the type of the variable by the value it is assigned. There is usually no need to specify the type of variables , the types of the variables can be inferred by the value being set. 
    Type Safety
  • Swift variables must have a type and must be initialized before they can be used. Conversion between different types is done explicitly; you cannot simply assign a float to an integer.
  • let doubleValue : Double = floatValue // This is an error
    Control Flow
  • The switch statement has undergone a major overhaul. Now it can select based not only integers, but also on strings, floats, and ranges of items, expressions, enums, and so forth. Moreover, there’s no implicit fall-through between case statements.
  • Also introduced a guard statement. A guard statement is like an if statement but with an additional requirement of always having an else statement. 
    Optionals
  • A variable will either be nil or it will have a valid value. The nil value is distinct from any valid value. Optionals can also be chained together to protect against errors and exceptions. 
    Unicode
  • The String and Character types are also fully Unicode compliant and support various encodings, such as  UTF-8, UTF-16, and 21-bit Unicode scalers.
    Other Improvements
  • Header files are no longer required.
  • Functions are full-fledged objects; they can be passed as arguments and returned from other functions. Functions can be scoped similarly to instance variables.
  • Comments can be nested.
  • There are separate operators for assignment (=) and comparison (==), and there’s even an identity operator (===) for checking whether two data elements refer to the same object.
  • There is no defined entry point for programs such as main.