1.var与let
//var 用来声明变量
var a = 40
a = 10
//let用来声明常量
let b = 23
//数据类型的推断,根据赋给值的类型来推断常用变量/常量的数据类型
var c : Int
c = 20
2.数据类型
(1).整型(Int)
//swift根据机器位数来决定值的范围:在32位机器上,大小就等于int32
//有符号8位整型 -128~127
var e : Int8 = 125
//无符号的8为整型 0~255
var f : UInt8 = 255
(2).float类型
var fa = 23.6 //swift默认将float推断成double类型
var fb : Float = 12.7
//在swift中,不能将数据类型进行隐式装换
var fc = fa + Double(fb)
(3).字符串
var str1 = "abcde"
var str2 = "f"
var str3 = str1 + str2//拼接字符串
str3.uppercaseString //将字符串转变为大写字母
str3.lowercaseString //小写转换
(4).Bool
swift中Bool类型的值只有true和false
(5).数组
//swift数组只能存放相同类型的数据,如果存放不同类型数据就会变成OC数组
//定义一个空数组
var allPeople = [String]()
allPeople = ["hanlei","lijipeng","zhabngyu","heyingjie"]
//添加元素
allPeople.append("dengxinyang")
allPeople.insert("lijipeng",atIndex: 1)
allPeople[0]
//删除元素
allPeople.removeAtIndex(5)
//修改
allPeople[1] = "jipeng"
//通过区间来取数组中的值
//包括2
allPeople[0...2]
//不包括2
allPeople[0..<2]
6.字典
//创建一个空字典
var peopleDic = Dictionary <String,Int>()
var peopleDic1 = ["hanlei" : 58,"limingfei" : 8]
//修改
//通过key值来修改字典中的值,如果key存在,修改;key不存在.添加.
peopleDic ["zhangyu"] = 38
peopleDic ["lijipeng"] = 21
peopleDic ["zhouxingyu"] = 25
peopleDic
peopleDic ["lijipeng"] = 213
peopleDic
//删除
peopleDic.removeValueForKey("lijipeng")
peopleDic
//根据index删除
let index = peopleDic.indexForKey("zhouxingyu")
peopleDic.removeAtIndex(index!)
peopleDic
//可选类型 ?,!
//nil:在swift表示值为空,表示没有值
// 在oc中表示空指针
//声明的value可以有值,也可以为nil
var value : Int? = 10
value = 100
7.元组
//用一个关系表举例的话,每一行就是一个元组,每列就是一个属性
//定义一个元祖
//在swift中,把多个值复合成一个类型,元组可以存放不同类型的值
var people = ("hanlei",38,"zhangyu",20)
people.0
people.1
//可以在定义元组时为属性起名
var people1 = (name : "zhangyu",age : 50)
people1.name
//可以直接为元组赋值
var(name,age) = people1
name
age