1 import Foundation 2 3 println("Hello, World!") 4 5 //let用来定义常量 6 let a = 10; 7 //a = 12; 8 9 var b: Int = 20; 10 b += a; 11 12 println("\(a) \(b)") 13 14 var d:Bool = false 15 16 println("\(d)") 17 18 //类型 19 //整形:Int 20 //浮点类型:Float Double 21 //布尔类型:Bool:true or false 22 23 //元组 24 let aa = (100,"hello world",1.5) 25 26 println(aa.0) 27 println(aa.1) 28 println(aa.2) 29 30 for(var i:Int = 0; i < 3 ; i++){ 31 switch(i){ 32 case 0: 33 println(aa.0) 34 case 1: 35 println(aa.1) 36 case 2: 37 println(aa.2) 38 default: 39 println("sssss"); 40 41 } 42 } 43 44 let bb = (name:"Gavin", age:22,QQ:"763303669",sex:"man",Tel:"18310060988") 45 46 let cc = (name:"name", age:"age" ) 47 48 //option可选变量,指的是:值可能为nil的变量 49 var aaa: Int? = nil 50 var bbb: Int! = 250 51 var ccc = bbb! + 100//!拆包:确定b存在值了可以进行运算了,无值的变量不能参与运算 52 53 println(bbb) 54 55 if ccc > aaa { 56 println("c大") 57 } 58 //?表示此变量为一个可选值 59 var ddd: Int? 60 61 if ddd { 62 println("C大") 63 } 64 65 //Switch的用法 66 var n = 0 67 68 switch n { 69 case 0: 70 println("hello tringx") 71 fallthrough 72 case 1: 73 println("激情为梦想而生") 74 fallthrough 75 case 2: 76 println("这是100") 77 fallthrough 78 default: 79 println("other") 80 } 81 //switch中...的用法 82 var m = 120 83 84 switch m { 85 case 0: 86 println("hello tringx") 87 case 1: 88 println("激情为梦想而生") 89 case 2...200: 90 println("这是一个大于2小于120的数") 91 default: 92 println("other") 93 } 94 //switch一个字符串 95 var l = "220" 96 97 switch l { 98 case 0: 99 println("hello tringx")100 case 1:101 println("激情为梦想而生")102 case 2,"220":103 println("这是2或220")104 default:105 println("other")106 }107 108 //switch在元组中的使用109 var k = ("232","123")110 111 switch k {112 case ("11","22"):113 println("hello tringx")114 //case (_,"123"):115 // println("忽略前者")116 case ("232",_):117 println("忽略后者")118 case ("232","123"):119 println("都不忽略")120 default:121 println("other")122 }123 124 //var character = ""125 126 for character in "hello-world"{127 println(character)128 }129 130 var While = 0131 while While < 10 {132 println(While)133 While++134 }135 136 var str: String = "www."137 println(str.isEmpty)//138 println(countElements(str))//计算字符个数或计算数组或字典的元素个数139 140 //字符串拼接141 var newStr = str + "tryingx" + ".cn"142 println(newStr)143 144 //字符串插入145 var str2 = "激情为梦想而生\(newStr)"146 println(str2)147 148 //判断是否以激情开头149 println(str2.hasPrefix("激情"))150 //判断是否以cn结尾151 println(str2.hasSuffix("cn"))152 //将str2中的左右小写字母转换为大写字母153 println(str2.uppercaseString)154 155 //数组156 //var arr1: Int = [10,8,13,9,17]157 //println(arr1)158 //打印数组的元素个数159 //println(countElements(arr1))160 161 //当数组中的元素不一致时162 var arr2 = [10,8,13,9,17,"hello"]163 println(arr2)164 165 var arr_1 = [1,2,3,4,5,6]166 var arr_2: Array= []167 arr_1[2] = 100168 println(arr_1[2])169 170 //171 arr_1[1...3] = [15]172 println(arr_1)173 174 //175 arr_1 += 20176 println(arr_1)177 178 //179 arr_1 += [5,8,2]180 println(arr_1)181 182 //183 arr_1.append(100)184 println(arr_1)185 186 //187 arr_1.insert(25,atIndex:1)188 println(arr_1)189 190 //删除数组中指定的元素191 arr_1.removeAtIndex(3)192 println(arr_1)193 194 //遍历数组195 for num in arr_1{196 println(num)197 }198 //应用于元组199 //待补充200 201 //字典202 var dict1 = ["one":"1","two":2,"three":3]203 println(dict1)//无序打印204 205 var dict2: Dictionary = ["one":1,"two":2,"three":3]206 println(dict2)207 //——————打印时——————208 209 //["one":1,"two":2,"three":3] Swift字典210 211 //OC字典212 //{213 // one = 1;214 // three = 3;215 // two = 2;216 //}217 218 //定义空字典219 var dict3: Dictionary = [:]220 println(dict3)221 dict3["one"] = 315222 println(dict3["one"])223 224 //修改元素225 dict3["one"] = 123226 println(dict3)227 228 //添加元素229 dict3["four"] = 4230 println(dict3)231 232 //删除元素233 dict2.removeValueForKey("two")234 println(dict2)235 236 //删除所有元素237 dict2.removeAll(keepCapacity: false)238 println(dict2)239 240 //遍历数组一241 for num in arr_1{242 println(num)243 }244 //遍历数组二245 for (index, value) in enumerate(arr_1){246 println("arr1[\(index)] = \(value)")247 }248 249 for (key,value) in dict2 {250 println("\(key):\(value)")251 }252 253 //函数254 func sum(x: Int,y: Int) -> Int{ //参数为Int,返回值为Int类型的函数255 return x + y256 }257 258 println(sum(10,20))259 260 func PI() -> Float { //参数为空,返回值为Float类型的函数261 return 3.1415926262 }263 264 func printTryingx(){265 println("激情为梦想而生")266 }267 268 println(printTryingx())269 270 func test(){271 272 }273 274 //参数为空,返回值为元组的函数275 func tupleFunction() -> (String, Int, Float){276 return ("TryingX",133364,9.123)277 }278 279 println(tupleFunction())280 281 //调用函数返回元组中的第几位元素282 var x = tupleFunction()283 println(x.0)284 285 //不定参数,返回值为Int类型的函数286 func sum2(numbers: Int...) -> Int {287 var sum = 0288 for num in numbers {289 sum += num290 }291 return sum292 }293 294 println(sum2(2,5,6,22,14,554,53))295 296 //局部参数名(本地参数名)297 298 func sum(num1 x: Int,num2 y: Int) -> Int{ //num1和num2不是用来计算的,而是用来外部调用的299 return x + y300 }301 302 println(sum(num1:10,num2:20))303 304 func sum1(#num_1: Int,num2 y: Int) -> Int{ //#num_1等同于num_1 num_1,即内部和外部参数名称一样时使用#修饰替代305 return num_1 + y306 }307 308 println(sum1(num_1: 10, num2: 20))//只要函数中添加了参数名,在调用时一定要加参数名309 310 func sum2(#num_1: Int,num2 y: Int = 50) -> Int{ //指定参数默认值311 return num_1 + y312 }313 314 println(sum2(num_1: 10))//在调用函数时如果不加入相对应的参数,将采用默认值进行运算315 316 //利用函数拼接字符串317 func strJoin(var leftStr str1:String,rightStr str2:String, joinStr join:String) -> String { //添加var后str1将成为可变变量,则可以进行str1 = "hello"的修改,去掉var则不能进行修改318 str1 = "hello"319 return str1 + join + str2320 }321 322 var str_1 = "阿根廷"323 var str_2 = "伊朗"324 325 println(strJoin(leftStr:"阿根廷", rightStr:"伊朗",joinStr:":"))326 327 //inout ?328 func strJoin1(inout leftStr str1:String, inout rightStr str2:String, joinStr join:String) -> String {329 str1 = "hello"330 return str1 + join + str2331 }332 333 var str_01 = "阿根廷"334 var str_02 = "伊朗"335 336 println(strJoin1(leftStr:&str_01, rightStr:&str_02,joinStr:":"))337 338 //函数嵌套339 func getValue(x: Int, y: Int) -> Int { //一个函数内部可以定义多个子函数340 func sum(a: Int, b:Int) -> Int { //sum函数为内部函数,只能在父函数内调用341 return a + b342 }343 func mul(a:Int,b:Int) -> Int {344 return a * b345 }346 347 return sum(x,y) + mul(x,y)348 }349 350 println(getValue(10,8))351 352 //函数的外部调用353 func minus(a: Int, b:Int) -> Int { //sum函数为内部函数,只能在父函数内调用354 return a - b355 }356 func mod(a:Int,b:Int) -> Int {357 return a % b358 }359 func getValue1(x: Int,y: Int,callBack:(Int,Int) -> Int) -> Int{360 return callBack(x,y)361 }362 363 println(getValue1(29,8,minus))364 println(getValue1(29,8,mod))365 366 //定义型函数(将函数名以参数的方式进行定义)367 var f:(Int,Int) -> Int = minus368 println(getValue1(10,8,f))369 370 //371 typealias CallBack = (Int,Int) -> Int//定义返回值类型(type类型定义)372 373 func returnFun(greatThanZero:Bool) -> CallBack {374 func sum(a:Int,b:Int) -> Int {375 return a + b376 }377 func mul(a:Int,b:Int) -> Int {378 return a * b379 }380 return greatThanZero ? sum : mul //调用执行内部函数381 }382 383 //var G = 10384 //var X = returnFun(G > 0)385 386 var Y = returnFun(true)//等同于上式387 388 println(Y(10,8))389 390 //闭包391 var arr = [10,13,214,34,41,56,75,43]392 393 //函数方法进行数组元素排序394 func compare(a: Int,b: Int) -> Bool {395 if a > b{396 return true397 }else{398 return false399 }400 }401 var sortedArr1 = sort(arr,compare)402 403 //闭包方式数组元素排序404 //简化版1:借助Swift的类型推断,我们可知:数组里面放的全是Int,因此sort函数的第二个参数也能推断出类型为(Int,Int) -> Bool,我们可以省略类型,省略后的格式如下:405 var sortedArr2 = sort(arr,{(a: Int,b: Int) -> Bool in return a > b})406 //简化版2:在闭包里,如果实现体只有一行代码,return单词可以省略,省略之后的格式如下:407 var sortedArr3 = sort(arr,{(a,b) -> Bool in a > b})408 //简化版3:在闭包里,可以根据参数的个数分别用$0,$1...替代第i个参数,因此可以转换为以下形式(可以去掉in之前的部分,$0可以替代a,$1可以替代b)409 var sortedArr4 = sort(arr,{$0 > $1})410 //简化版4:因为Swift支持操作符重载,并且$0代表第一个参数,$1代表第二个参数,因此可以省略操作符左右的参数,简化格式如下:411 var sortedArr5 = sort(arr,>)412 413 println(sortedArr1)414 println(sortedArr2)415 println(sortedArr3)416 println(sortedArr4)417 println(sortedArr5)418 419 //带小数的数组使用闭包排序420 var array = ["8.1","23","44","65","125","7.8"]421 var sortedArr6 = sort(array,>)422 println(sortedArr6)423 424 //伪闭包425 var sortedArrWei = sort(arr){426 $0 > $1427 }428 println(sortedArrWei)