Apple Introduced new programming language Swift and its available with new Xcode 6. I have not any experience in Swift language but i want to learn it as a developer also i would like to share with you , may be it will helpful to you too :) So here is my first post about Swift Language.
NOTE : Its better to open playground in Xcode will help to execute swift code easily and will get the result immediately.
example to open Playground
Swift is entirely different from Objective C language, I will go step by step intro to Swift.Swift Basic
In swift we need to use 'let' keyword to make a constant and 'var' keyword for variable. As you know that a constant cannot be changed once it is set whereas a variable can be set to a different value in the future.
Here is the example
let aConstant = 10
var aVariable = 20
aVariable = 25
Here the compiler automatically conclude that the variables are in int. However you can explicitly mention the type by writing it after the variable with a colon. This only necessary if the value doesn't provide enough information or there is no initial value.
Ex:
let aDouble = 10.1
let anInt = 10
let anotherDouble:Double = 10
If you need to convert to another type, you should explicitly mention , see the example below.
let aString = "It's time to use Xcode"
let versionNo = 6
let title = aString +String(versionNo)
Here the keyword String will convert the versionNo type to string value.
Output
"It's time to use Xcode 6"
There is more easiest way to convert a string value
Ex:
let aFloat = 4
let bFloat = 6
let aString = "Total of \(aFloat ) and \( bFloat) value is \(aFloat+bFloat)"Next Chapter : Creating Array and dictionary in Swift
No comments:
Post a Comment