DataType

//: Playground – noun: a place where people can play

// Swift2 DataType (constant, variable)

// Swift2 DataType (int, float, double, char, string, tuple, type inference, type casting)

// Kyoung Shin Park (2016 Fall)

import Cocoa

// constant

let x = 16 // Int (type inference)

let y : Double = -2.3 // Double

let z = -1.2 // Double (type inference)

let w : Float = -1.7 // Float

let str = “Hello, playground” // String (type inference)

let c1 = “A” // String (type inference)

let c2 : Character = “A” // Character

print(“x=\(x) y=\(y) z=\(z) w=\(w) y+z=\(y+z)”)

print(“x+y+z+w=\(Double(x)+y+z+Double(w))”) // type casting

print(“str=\(str) c1=\(c1) c2=\(c2)”)

// tuple

let (s, t, r, u, v) = (“ABC”, “D”, 1, 3.5, true) // tuple

print(“s=\(s) t=\(t) r=\(r) u=\(u) v=\(v)”)

let tupleValues = (“ABC”, “D”, 1, 3.5, true) // tuple

print(“\(tupleValues.0) \(tupleValues.1) \(tupleValues.2) \(tupleValues.3) \(tupleValues.4)”)

// variable

var isUsed = true // Bool (type inference)

if isUsed {

print(“isUsed=\(isUsed)”)

}

var a = 10 // Int (type inference)

print(“a=\(a)”)

a = 20

print(“a=\(a)”)

var b : UInt // UInt declaration

b = 30 // initialization

print(“b=\(b)”)

print(“a+b=\(a+Int(b))”)

for i in a…Int(b) {

print(i)

}

for j in 0..<5 {

print(j)

}

var str1 = “Hello” // String (type inference)

print(“str1=\(str1)”)

str1 += ” World” // String += operator

print(“str1=\(str1)”)

for s in str1.characters { // using foreach

print(“\(s)”)

}

for i in 0..<str1.characters.count { // using String.Index & range

print(“str1[\(i)]=\(str1[str1.startIndex.advancedBy(i)])”)

}

var charView = str1.characters

for (index, value) in charView.enumerate() { // using array & tuple

print(“str1[\(index)]=\(value)”)

}