学习Swift第一天的笔记
1 import Foundation 2 3 println("Hello, World!") 4 5 /* 6 let 定义常量,var 定义变量 7 */ 8 var hello = "Hello, World!" //定义一个变量 9 var helloWorld: String = "Hello, World!" //一般情况下Swift会自动推断其所属类型,如果想要显示的指定其类型,就在变量名后面跟“: 类型名” 10 helloWorld += "!" //借鉴了C++里面的运算符重载,追加字符串 11 println(helloWorld) //后台打印变量"hello"的值 12 println("hell0 = \(hello)") //在打印的字符串中引入变量 13 14 var 你好 = "你好" //Swift中支持几乎所有的Unicode字符,除了数学上的符号、剪头、横杠以及系统关键字以外 15 let 动物园 = "zoo" 16 println("\(你好),\(动物园)") //后台打印中文字符的变量,并进行变量拼接 17 18 let 动物 = "animal"; let 猴子 = "monkey" //单行创建多个变量或常量其间需要使用";"隔开 19 20 let a: Int8 = 8 //整形会自动推断为Int类型(在32位机器上是Int32,在64位机器上是Int64),如果想声明为其他类型,则需要显示指定其类型。 21 22 let b = 12.0 //浮点型数据会自动推断为Double类型。 23 let c: Float = 16.0 //指定浮点型变量为Float类型需要显示指定。 24 //let d = a + b //两个不同数据类型的常量/变量不能直接进行运算,因为Swift中没有隐式转换,必须开发者显示转换。 25 let d = Double(a) + b //添加显示转换的两个不同类型的变量间运算,显示转换方式:类型名(常量/变量) 26 27 /* 28 元组分解 29 */ 30 let http404ErrorA = (404, "httpRequestError") 31 println("errorCode = \(http404ErrorA.0)") //通过下标分解元组 32 println("errorCode = \(http404ErrorA.0), errorMessage = \(http404ErrorA.1)") //通过下标分解元组并拼接字符串 33 34 let http404ErrorB = (errorCode:404,errorMessage:"httpRequestError") 35 println("errorCode = \(http404ErrorB.errorCode),errorMessage = \(http404ErrorB.errorMessage)") 36 37 38 /* 39 数组,一个数组里只能存储一种类型的数据 40 */ 41 42 var nameArrayA:String[] = ["zhangsan","lisi","wangwu"] //显示表示字符串数组 43 var nameArrayB = ["zhangsan","lisi","wangwu",32] //显示表示字符串数组 44 var nameArrayC = [12,22,32,43] 45 46 println("nameArrayA = \(nameArrayA)")// 47 println("nameArrayB = \(nameArrayB)")// 48 println("nameArrayC = \(nameArrayC)")// 49 50 println("nameArrayA = \(nameArrayA),nameArrayB = \(nameArrayB)") 51 52 nameArrayA.append("32") //Swift中的数组内只能拼接同一种类型的数据 53 nameArrayA.removeAtIndex(0) //Swift中移除数组中的一条数据,remove后只是删除了相应字段的内容值,但不会删除字段 54 nameArrayA[0] = "xingxing" //Swift中修改数组中的一条数据 55 println("nameArrayA = \(nameArrayA)") 56 57 //循环遍历数组(两种方法) 58 for name in nameArrayA{ 59 println("name = \(name)") 60 } 61 for (index, name) in enumerate(nameArrayA){ 62 println("index = \(index),name = \(name)") 63 } 64 65 var person:Dictionary<String, String> = ["name":"zhangsan","sex":"m"] //Swift中定义字典 66 println("\(person)") 67 68 //添加数组成份 69 person["habit"] = "football" 70 person["name"] = "gavin" 71 person["sex"] = "m"; 72 println("person1 = \(person)") 73 74 //添加具有相同字段名的成份 75 person["name"] = "xiaoli" 76 person["sex"] = "w" 77 person["habit"] = "read" 78 println("person2 = \(person)") 79 80 //更新数组中相应字段名的数据 81 let oldName = person.updateValue("baobei",forKey:("name")) 82 println("person = \(person), oldName = \(oldName)") 83 84 //循环遍历字典 85 for (key, value) in person{ 86 println("key = \(key), value = \(value)") 87 } 88 /* 89 数组可以进行更新,字典不可以更新 90 */ 91 92 var condition = true 93 var number = 1 94 95 /* 96 if条件语句 97 */ 98 //错误if条件语句 99 //if number { //if条件语句不需要添加小括号,判断条件必须为bool类型数据 100 // println("do something") 101 //} 102 //正确if条件语句 103 if condition { //if条件语句不需要添加小括号,判断条件必须为bool类型数据 104 println("do something") 105 } 106 /* 107 for循环 108 */ 109 for var i = 0; i < 100; i++ { 110 print("\(i) ") //不换行打印 111 println("i = \(i)") //换行打印 112 } 113 114 /* 115 switch语句 116 */ 117 var s = 5 118 switch s{ 119 case 1: 120 println("s = 1") 121 // fallthrough //强制执行下一句 122 case 2..6: //范围运算符“1..10”:只包括1,不包括10,“1...10”:1和10都包括 123 println("s = 2") 124 // fallthrough 125 case 3..11 where s==10: //双重判定 126 println("s = 3") 127 // fallthrough 128 default: 129 println("s = other number") 130 } 131 132 var point1 = (3, 4) 133 134 switch point1 { 135 case (0, 0): 136 println("origion") 137 case (0,_): //“_”忽略这个位置的值 138 println("y axis") 139 case (_,0): 140 println("x axis") 141 default: 142 println("in range") 143 } 144 var point2 = (0, 4) 145 146 switch point2 { 147 case (0, 0): 148 println("origion") 149 case (0,let y): 150 println("y axis, y = \(y)") //输出y轴上的值 151 case (_,0): 152 println("x axis") 153 default: 154 println("in range") 155 } 156 157 /* 158 ——————————optionals可选变量———————————— 159 1.nil:所有类型为空均可使用 160 2.optionals声明 161 3.optionals拆包(Forced Unwrapping) 162 */ 163 164 var value: Int?//“?”用在数据类型之后代表为可选类型,值可能为nil 165 166 //将字符串转换为Int类型 167 let numberStr = "12345" 168 value = numberStr.toInt() //value返回一个bool类型 169 170 //解包 171 if value{ 172 println("value = \(value!)") //"!"表示解包,表示将value中的值取出 173 var acceptValue: Int = value! //使用“!”可将value中的值取出并赋给其他变量 174 }else{ 175 176 } 177 /* 178 函数:func 函数名 (参数列表) 返回值 { 179 实现体 180 } 181 */ 182 func sayHello(name: String) -> String{ 183 return "Hello" + name 184 } 185 186 println(sayHello("zhangsan")) 187 188 //函数的类型:参数类型+返回值类型 189 190 var funcType1:(String) -> String = sayHello //定义一个名称为"funcType"的函数 191 192 func sayGoodBye(name: String) -> String{ 193 return "GoodBye" + name 194 } 195 196 println(funcType1("lisi")) 197 198 funcType1 = sayGoodBye 199 200 var funcType2:(String) -> String = sayGoodBye 201 202 println(funcType1("lisi")) 203 println(funcType2("lisi")) 204 205 func count(name: String) -> (vowels: Int, consonant: Int) 206 { 207 var vowels = 0, consonants = 0 208 for character in name{ 209 var string: String = String(character).lowercaseString 210 switch string{ 211 case "a","e","i","o","u": 212 vowels++ 213 case "b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z": 214 consonants++ 215 default: 216 println("非字母") 217 } 218 } 219 return(vowels, consonants) 220 } 221 println(count("woshixingxing")) 222 223 var array = count("我是xingxing") 224 println("vowels = \(array.0) consonants = \(array.1)") 225 226 /* 227 局部变量和外部变量 228 */ 229 230 func join1(string1: String, string2: String) -> String{ 231 return string1 + string2 232 } 233 234 println(join1("Hello"," Xiaoxingxing")) 235 236 func join2(externalName1 string1: String, string2: String) -> String{ 237 return string1 + string2 238 } 239 240 println(join2(externalName1:"Hello"," Xiaoxingxing")) //"externalName1"为外部变量名,"string1"为局部变量名 241 242 func join3(#string1: String, #string2: String, #joiner:String) -> String{ 243 return string1 + joiner + string2 244 } 245 println(join3(string1:"Hello",string2:"xingxing",joiner:"-")) 246 247 func join4(#string1: String, #string2: String, joiner: String = "-") -> String{ 248 return string1 + joiner + string2 249 } 250 println(join4(string1:"Hello",string2:"xingxing")) 251 println(join4(string1:"Hello",string2:"xingxing",joiner:"*")) 252 //println(join3(string1:"Hello",string2:" Xiaoxingxing")) //在"string1"和"string2"前加”#“,"string1"和"string2"既为局部变量名也是外部变量名 253 254 func join5(#string1: String, #string2: String,_ joiner: String = "-") -> String{ //”_“取消其外部名称 255 return string1 + joiner + string2 256 } 257 println(join5(string1:"Hello",string2:"xingxing")) 258 //println(join5(string1:"Hello",string2:"xingxing",joiner:"#")) 259 println(join5(string1:"Hello",string2:"xingxing","#")) 260 261 func caculateMeal(numbers: Double ...) -> Double 262 { 263 var sum = 0.0 264 for number in numbers{ //获取每一个参数的值 265 sum += number 266 } 267 return sum/Double(numbers.count) 268 } 269 270 println("meal = \(caculateMeal(1.0,2.0,3.0))") 271 272 func incream(var a: Int,var b: Int) 273 { 274 a++ 275 } 276 277 var increamNumber = 5 278 incream(increamNumber, 3) 279 println("increamNumber = \(increamNumber)") 280 281 func swapTwoInt(inout a: Int,inout b: Int) //”inout“为可修改关键字,即:传进的参数可以进行修改 282 { 283 var temp = a 284 a = b 285 b = temp 286 } 287 288 var swapValueA = 3,swapValueB = 5 289 swapTwoInt(&swapValueA, &swapValueB) //传变量地址进行修改 290 println("swapValueA = \(swapValueA), swapValueB = \(swapValueB)") 291 292 293 /* 294 函数嵌套 295 */ 296 //外部函数 297 func stepBackward(number: Int) -> Int 298 { 299 return number - 1 300 } 301 302 func stepForward(number: Int) -> Int 303 { 304 return number + 1 305 } 306 307 //内部嵌套函数 308 func changeValue(isStepBackward: Bool) -> (Int) -> Int 309 { 310 //被嵌套的函数 311 func stepBackward(number: Int) -> Int 312 { 313 return number - 1 314 } 315 316 func stepForward(number: Int) -> Int 317 { 318 return number + 1 319 } 320 return isStepBackward ? stepBackward : stepForward 321 } 322 323 var changeValueFunc: (Int) -> Int = changeValue(true) 324 var changedValue = 3 325 println("after changed: changedValue = \(changeValueFunc(changedValue))") 326 //——————————闭包—————————— 327 var names = ["zhangsan","lisi","wangwu","zhaoliu"] 328 329 //var sortedNames = sort(names,{ 330 // (stringA: String ,stringB: String) -> Bool in return stringA > stringB 331 //}) 332 333 //自动类型推断 334 var sortedNames = sort(names,{ 335 (stringA ,stringB) -> Bool in return stringA > stringB 336 }) 337 println("sortedNames = \(sortedNames)") 338 339 //var sortedNmaes = sort(names,{stringA,stringB->Bool in stringA > stringB}) 340 341 //var sortedNmaes = sort(names,{$0>$1}) 342 343 var sortedNmaes = sort(names,>) 344 345 //如果函数中有闭包的话就把闭包放到外面 并且闭包参数是最后一个 例如: 346 var sortedNmaes1 = sort(names){ 347 $0>$1 348 }
激情为梦想而生