Monday, 27 February 2017

String : language representation







    let dogString = "Dog‼🐶"

// utf-8
print(" *** UTF-8 *** ")
for codeUnit in dogString.utf8 {
    print("\(codeUnit) ", terminator: "")
}
print("")


// utf-16
print(" *** UTF-16 *** ")
for codeUnit in dogString.utf16 {
    print("\(codeUnit) ", terminator: "")
}
print("")

// Scalar
print(" *** Unicode Scalar Representation *** ")
for scalar in dogString.unicodeScalars {
    print("\(scalar) ", terminator: "")
}
print("")

Sunday, 26 February 2017

Prefix and Suffix





let line = "0001 Some test data here %%%%"
print(line.hasPrefix("0001"));    // true
print(line.hasSuffix("%%%%"));    // true

String insert, remove, compare



1. Insert
//insert
    var welcome = "hello";
print(welcome);
    welcome.insert("!", at: welcome.endIndex);
    // welcome now equals "hello!"
     print(welcome);
    welcome.insert(contentsOf:" there".characters, at: welcome.index(before: welcome.endIndex));

    // welcome now equals "hello there!"

2. Remove
// remove
    welcome.remove(at: welcome.index(before: welcome.endIndex));
    // welcome now equals "hello there"
     print(welcome);
    let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
    welcome.removeSubrange(range);
print(welcome);
    // welcome now equals "hello"

// String and character equality

3. Compare

    let quotation = "We're a lot alike, you and I."
    let sameQuotation = "We're a lot alike, you and I."
    if quotation == sameQuotation {
        print("These two strings are considered equal")
    }
    // Prints "These two strings are considered equal"

String Indics



Each String value has an associated index type, String.Index, which corresponds to the position of each Character in the string.




    let greeting = "Guten Tag!";

    print(greeting[greeting.startIndex]);
    // G
    print(greeting[greeting.index(before: greeting.endIndex)]);
    // !
    print(greeting[greeting.index(after: greeting.startIndex)]);
    // u
    let index = greeting.index(greeting.startIndex, offsetBy: 7)
    print(greeting[index]);
    // a

String basic




var s1 = "One";
var s2 = "Two";
var s3 = "Three";
var emp = "";

// normal String print
print(s1);

// check string is empty
//1.
if emp.isEmpty{
print("This is empty string");
}else{
print(emp);
}
//2.
if s2.isEmpty{
print(" This is empty string");
}else{
print(s2);
}

// String constant
let b = String(" this is constant value");
//b = b+"var value";  // when uncomment - show error
print(b);

// String concatenation
print(s1+s2);

// count
print( s3.characters.count);
print(s3.utf16.count);

Control Statement




// for in : loop to iterate over a sequence, such as ranges of numbers
for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
if index == 4
{
break;
}
}


// while statement

//  fallthrough statement
var j  = 8;
switch(j)
{
 case 8:
    print(" value - 8")
fallthrough
case 16:
print( "16 value");
fallthrough
default:
print( "default value");

}

Loop : for and while




// for in : loop to iterate over a sequence, such as ranges of numbers
for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
}

//for in : If you don’t need each value from a sequence, you can ignore the values by using an underscore in place of a variable name. 
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
    answer *= base
}
print("\(base) to the power of \(power) is \(answer)")


//Use a for-in loop with an array to iterate over its items. 
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
    print("Hello, \(name)!")
}

// (key, value) tuple’s members as explicitly named constants for use within the body of the for-in loop.
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
    print("\(animalName)s have \(legCount) legs")
}

// while statement
var i  = 0;
while(i < 10)
{
    print(i);
    i = i+1;
}

// repeat while statement
var j  = 0;
repeat
{
    print(j);
    j = j+1;
}while(j < 10)

Saturday, 25 February 2017

Decision - making statement




var i = 25;
var j = 5;
var k = 45;
var total = 0;
var status = true;
// if statement
if i != 5
{
print(i);
}

// if...else statement
if k == 5
{
print(k);
}else{
print("This is not pass the equality challenge, Try Again!");
}

// if...else statement
if k == 5
{
print(k);
}else if k == 45{
print(k);
}else{
print("This is not pass the equality challenge, Try Again!");
}

// if...else statement
if i<k
{
if j < k
{print(k);}
}else{
print("This is not pass the equality challenge, Try Again!");
}

// ternary / conditional operator
print(status ? i : "This is false!");

// switch statement
switch(k)
{
case 4 :
print("value of k : \(k)") ;
break;

    case 45 :
print("value of k : \(k)");
break;

default :
print("There are no value.") 
break;

}

Operator



// arithmatic operator

var i = 25;
var j = 5;
var k = 45;
var total = 0;

print(i+j);
print(i-j);
print(i*j);
print(i/j);
print(i%j);

// comparison operator
print(i < j);
print(i > j);
print(i <= j);
print(i >= j);
print(i != j);
print(i == j);

// logical operator
print((i > j) && (j < k));
print((i != j) || (j < k));
print(!(i > j) && (j < k));

// bitwise operator
print(i&j);
print(i|j);
print(i^j);
print(~k);

// assignment operator
print(total = i + j);
print(i += j);
print(i -= j);
print(i *= j);
print(i /= j);
print(i %= j);
print(i <<= j);
print(i >>= j);
print(i &= j);
print(i |= j);
print(i ^= j);

// range operator
print(j...k);
print(j..<k);

Literals




// INTEGER LITERAL
let decimalInteger = 17           // 17 in decimal notation
let binaryInteger = 0b10001       // 17 in binary notation
let octalInteger = 0o21           // 17 in octal notation
let hexadecimalInteger = 0x11     // 17 in hexadecimal notation

print("First : \(decimalInteger), Second : \(binaryInteger), Third : \(octalInteger), Fourth : \(hexadecimalInteger)");

// FLOAT LITERAL
let decimalDouble = 12.1875
let exponentDouble = 1.21875e1
let hexadecimalDouble = 0xC.3p0

print("First : \(decimalDouble), Second : \(exponentDouble), Third : \(hexadecimalDouble)");

// STRING LITERAL
let stringL = "Hello\tWorld\n\nHello\'Swift\'"
print(stringL)

// BOOL LITERAL
let lightOn = true
print(lightOn)

Constant





1. 
 let constA = 42
print(constA)


2.   

// TYPE ANNOTATION
let constB:Float = 3.14159

print(constB)



3. 
let constA = "Godzilla"
let constB = 1000.00

print("Value of \(constA) is more than \(constB) millions")

Optional


1.  Optional

var name : String? = "nil";

if name != nil{
    print(name);
}else{
    print("This is a nil string.");
}



2. Automatic Unwrapping

var myString:String!

myString = "Hello, Swift!"

if myString != nil {
   print(myString)
}else {
   print("myString has nil value")
}




3. Optional Binding

var myString:String?

myString = "Hello, Swift!"

if let yourString = myString {
   print("Your string has - \(yourString)")
}else {
   print("Your string does not have a value")
}


Answer : Your string has - Hello, Swift!




22

Type Safety




var METER = 55;

var METER = "This is String";

print(METER);


Error : ERROR at line 4, col 5: invalid redeclaration of 'METER'
var METER = "This is String";
    ^
NOTE at line 2, col 5: 'METER' previously declared here
var METER = 55;
    ^

type alias : User define data type




typealias METER = Int32;

let distance : METER = 2000;

print(distance);

DataType




// *******************DATATYPE

// SIZE =     RANGE =

// SIZE = 1byte    RANGE = -127 to 128
let i : Int8 = -127;
// SIZE = 1    RANGE = 0 to 255
let iU : UInt8 = 255;
// SIZE =     RANGE =
let j : Int16 = -12000;
// SIZE =     RANGE =
let jU     : Int16 = 12000;
// SIZE = 4 byte    RANGE = -2147483648 to 2147483647
let k : Int32 = -2147483648;
// SIZE = 4byte   RANGE = 0 to 4294967295
let kU : UInt32 = 4294967295;
// SIZE = 8byte    RANGE = -9223372036854775808 to 9223372036854775807
let l : Int64 = -9999999999;
// SIZE = 8byte     RANGE = 0 to 18446744073709551615
let lU : UInt64 = 18446744073709551615;

// SIZE = 8byte     RANGE = 0 to 18446744073709551615
let fU : Float = 18446744073709551615.880099;
// SIZE = 8byte     RANGE = 0 to 18446744073709551615
let dU : Double = 18446744073709551615.9977886655;

let s : String = "This is String";
let c : Character = "A";
let z :

print(i);
print(iU);
print(j);
print(jU);
print(k);
print(kU);
print(l);
print(lU);
print(fU);
print(dU);
print(s);
print(c);

























Friday, 24 February 2017

Run and compile code


To run and compile
---------------------------------

1.   https://iswift.org/playground

2.   https://swift.sandbox.bluemix.net/#/repl

First Swift Program





var program = "Hello SWIFT, This is my first program"

print(program);

// another way

print("Output is : ", program)