swift学习日志-1基础篇 常量变量 布尔值 元组

基础篇

常量和变量

常量和变量必须在使用前声明,用let来声明常量,用var来声明变量。


let constant = 10
var varible = 20
 
swift里变量类型都是系统自动判断,也可以自己定义 
let constant :int = 10
var varible :int = 20
 

常量的值一旦被确定就不能更改了

输出

println(constant)//输出“10”
 
print(constant)//同样也输出“10”,只是不会换行
 
 
数值型类型转换

let num1: UInt16 = 2_000
let num2: UInt = 1
let num3 = num1 + UInt16(num2)
 
 
let three = 3
let floatnum = 0.14159
let pi = Double(three) + floatnum

 

布尔值

true 和 false

let x = 1
if x{
    //x不是布尔类型,会报错
}
 
if x == 1{
   //通过编译
}
 
let x = true
if x {
   //通过编译
}

元组

把多个任意类型的值组合成一个复合值

let tuples = (7,"hello")//tuples 的类型为(int, string)

元组内容分解

let(num,strings)= tuples
println("the status code is \(num)")//输出the status code is 7
println("the status message is \(strings)")//输出 the status message is hello

 访问单个元组内元素,下标从 0 开始 

println("the status code is \(tuples.0)")//输出 the status code is 7
println("the status message is \(tuples.1)") //输出 the status  message is hello

可以在定义元组时给单个元素命名

let asdas = (statusCode: 200, description: "OK")








                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值