您的位置:首页 > 移动开发 > Swift

swift 学习笔记 - 数组,字典,元组

2015-10-30 14:12 337 查看
var array = ["a","b","c","d"]   //声明一个数组

array[0] = "A"

array.append("e")               //在数组末尾添加一个元素

array.removeAtIndex(0)          //删除数组中第1个元素,从0开始算起

array += ["f"]                  //在数组末尾添加一个元素

array.removeFirst()             //删除数组中第1个元素

array.removeLast()              //删除数组中最后一个元素

array.insert("A", atIndex: 0)   //向数组中的某个位置插入一个元素




<pre name="code" class="plain">//声明一个字典。声明时,Value不可为nil</span>
<span style="font-family: Arial, Helvetica, sans-serif;">var dict = ["name":"jiluanxi","age":22,"sex":"boy"]</span>
dict["tel"] = 18360538622       //增加一个键值对

dict["tel"] = nil   //将一个Key对应的Value置为nil,会默认删除

dict.removeValueForKey("sex")   //删除对应Key的Value值

dict.updateValue("Jeason", forKey: "name")  //修改对应Key的Value值

var name = dict["name"]         //取出对应Key的Value值

print("My name is \(name!)")    //可选类型用!来确认





//声明一个元组,元组中可以存放不同类型的值
var student = ("jiluanxi",22,18360538622,"boy")

var stuName = student.0         //取元组中的数据

var stuAge = student.1

var stuTel = student.2

var stuSex = student.3

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  swift