// // main.swift // 数组 // // Created by Mac on 15-9-9. // Copyright (c) 2015年 青苹果. All rights reserved. // import Foundation //不可变数组,用let定义即可 var array:[String] = ["ss","set","jim"] println("\(array)") var array1:[Float] = [23,45,36]; println("\(array1)") //定义一个空的数组 var array2:[String] = [] println("\(array2)") var array3 = [array,array1,array2] //数组内添加内容 array += ["nme"] println("\(array)") var a = "shei" array += [a] array += [a] array += [a] array += [a] println("\(array)") array.append("shine") println("\(array)") //修改数组内某个元素 array[0] = "ikmage" println("\(array)") //修改指定位置,指定长度的元素 array[0...1] = ["sine","kmse"] println("\(array)") //指定位置插入元素 array.insert("zhang", atIndex: 2) println("\(array)") //删除指定位置的元素 array.removeAtIndex(2) println("\(array)") //删除最后一个元素 array.removeLast() println("\(array)") 删除指定位置指定长度的元素 //array.removeRange(Range(0...3)) //println("\(array)") // 删除所有元素 //array.removeAll(keepCapacity: true) //println("\(array)") //数组的遍历,方式一 //先定义元素,在遍历for in方法遍历 var a1 = "" for a1 in array { println("\(a1)") } //for循环遍历 for var i = 0;i<array.count;++i { println("\(array[i])") } //显示健值对的遍历 for(index,value) in enumerate(array) { println("\(index):\(value)") } //不可变数组的定义 let array4:[String] = ["seni","wlsi"] println("\(array4)") var a2 = "" for a2 in array4 { println("\(a2)") }
字典:
// // main.swift // 字典 // // Created by Mac on 15-9-9. // Copyright (c) 2015年 青苹果. All rights reserved. // import Foundation //字典是无序的 var dic:[String:String] = ["key1":"value1","key2":"value2"] println("\(dic)") var a1:(NSInteger) = dic.count //移除指定key的健值队 dic["key1"] = nil println("\(dic)") //添加健值对 dic["key1"] = "value1" println("\(dic)") //修改指定key的value dic["key1"] = "zhang" println("\(dic)") //创建一个空的字典 var dic1:[String:String] = [:] println("\(dic1)") //判断健值对是否为空,然后移除 if var key = dic.removeValueForKey("key1") { dic.removeValueForKey("key1") println("\(dic)") } //判断健值对是否存在 if var key = dic["key1"]{ println("\(key)") }else{ println("空") } //移除所有健值对 //dic.removeAll(keepCapacity: true) //println("\(dic)") //字典的遍历,字典是无序的 for (key,value) in dic{ println("\(key):\(value)") } var a5 = [:] for a5 in dic{ println("\(a5)") } //遍历所有的key for key in dic.keys{ println("\(key)") } var a3 = "" for a3 in dic.keys{ println("\(a3)") } //遍历所有的value for value in dic.values{ println("\(value)") } var a4 = "" for a4 in dic.values{ println("\(a4)") } //取出所有的key var arrayKey = Array(dic.keys) println("\(arrayKey)") //取出所有的value var arrayValue = Array(dic.values) println("\(arrayValue)")
循环得使用方法:
// // main.swift // 循环练习-01 // // Created by Mac on 15-9-8. // Copyright (c) 2015年 青苹果. All rights reserved. // import Foundation var j = 0 //for循环得结构方式 for j in (0...15){ println("\(j)") } for var k = 0;k<100;++k{ println("\(k)") } for var k1 = 0;k1<100;k1=k1+2{ println("k1 ==\(k1)") } //创建不包含上限得范围 for var i1 = 0;i1>=0;++i1{ if i1 > 1000{ break } println("i1=\(i1)") } //while循环 var i2 = 0 while i2<100{ i2++; println("i2=\(i2)") } //do while循环 var i3 = 0 do{ i3++ println("i3=\(i3)") }while i3<100 var i4=0 for var j1=0;j1<10;j1=j1+1{ for var j2=0; j2<10; ++j2{ i4++; println("\(i4)") } }