swift Data type

Int

Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64

var intmax  = Int.max
println("Int.max=\(intmax)")

Int, String

Int to String

var i = 100
var strI = String(i)
var numA = Double(i)
var numB = 100.0
var intA = Int(numB)

String to Int

toInt

var str = "100"
var i = str.toInt()

var str2 : String  = "200"
var i2 : Int? = str2.toInt()

With check

var numStr : String = "A"
var i : Int? = numStr.toInt()
if (i == nil) {
  i = 0
}

String

Format

var app = "Angry Bird"
var str = "I love \(app)."
println(str)

Optional

Optional value ?

If set nil, error has happens.
? : It has possibility to set nil.

// var notnill : String = nil  // Error
var nilstr : String? = nil
// var nill : String = nilstr  // Error

To use optional value, add ? after value name

var str : String? = "abcmart"
var newstr : String? = str?.uppercaseString

Optional value !

This is not nil.

var str : String! = "AB"
str = nil
var mystr : String = str  // Crash!!!

nil check

Optional Biding

int let tmp = str {
  // str is not nil
}
else {
  // str is nil
}

??

var i : Int? = nil
var refi : Int = i ?? -1  // if i is not nil, insert i, nil set -1

Swift enum

enum TYPE: Int {
  case A = 0
  case B
  case C
  case D
}