Swift 基本常量和变量,基本数据类型

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

 

import UIKit

 

var str = "Hello, playground"

 

 

//常量  let 

let maxNum = 1000

//maxNum = 2000

 

 

//变量  var

var index = 2

index = 3

 

 

var x = 1, y = 2, z = 3

 

 

//Type inference

//x = "Hello"

 

var s = "Hello"

 

let webSite: String = "www.imcooc.com"

 

//整型

 

var imIntt: Int = 80

Int.max

Int.min

 

var imUInt: UInt = 50

UInt.max

UInt.min

 

UInt8.max

UInt8.min

 

UInt32.max

UInt32.min

 

Int64.max

Int64.min

 

//浮点数

 

let imFloat:Float = 3.1415927

let imDouble:Double = 3.1415926

 

var  a = 1.25e10

var b = 1.25e-8

 

 

//不同类型转换

 

let x: UInt16 = 100

let y: UInt8 = 20

//x+y//swif不能自动转换类型

let m = x + UInt16(y)//必须类型强制转换

 

 

let a:Double = 3.0

let b:Float = 0.3

a + Double(b)

Float(a) + b

 

let w: Float = 3

let v:Int = Int(3.2)

 

 

let integer = 3

let fraction = 0.1415926

let pi = Double(integer) + fraction

 

let red:CGFloat = 0.2

let green:CGFloat = 0.5

let blue:CGFloat = 0.5

 

 

 

 

 

UIColor(red: red, green: green, blue: blue, alpha: 1)

 

 

 

let imTure: Bool = true

let imFalse = false

 

if imTure {

    print("I'm True")

} else if 3 + 4 == 7 {

    print("3 + 4 == 7")

}

else {//会出现警告,代码冗余(不会被执行)

    print("I'm False")

}

 

//if 1 {//不可以这样写,整型和布尔值不能混合使用

//    print("True")

//}

 

let a  = 1

if a == 1 {

    print("True")

}

 

 

元组

 

var point  = (5, 2)

var httpResponse = (404 , "Not Found ")

 

var point2: (Int , Int , Int) = (10, 5, 2)

var  httpResponse2:(Int, String) = (200, "OK")

 

//元组使用

let (x, y) = point

x

y

 

 

var (statusCode ,statusMessage) = httpResponse

statusCode

statusMessage

 

statusCode = 405

 

 

 

point.0

point.1

 

 

let point3 = (x:3, y:4)

point3.x

point3.y

 

let point4: (x:Int, y:Int) = (10,5)

point4.x

point4.y

 

 

let loginResult = (true, "liuyubobobo")

let  (isloginSuccess, _) = loginResult//获取第一个参数,不需要第二个参数用_代替

 

if isloginSuccess {

    print("Login success !")

}

else {

    print("Login failed !")

}

 

 

 

 

//String 字符类型

 

//使用双引号

 

var 名字 = "liuyubobobo"

print("我的名字是" + 名字)

 

//print 函数

 

let x = 1, y = 2, z = 3, b = true

 

print(x, y, z, b)

print(x, y, z, separator: ",")//separator 打印的信息分割符(相当于空格)

print(x, y, z, separator: "--")

print(x, y, z, separator: "--", terminator: ":)")//terminator 相当于回车

print("Hello")

 

 

 

print(y , "*", z, "=", y*z)

print("\(y) * \(z) = \(y*z)")

 

//注释

/**/  //多行注释

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

= "www.imcooc.com"

 

posted @ 2016-11-01 15:38  一枚IT女  阅读(351)  评论(0编辑  收藏  举报