Swift学习第一天--面向过程
//: Playground - noun: a place where people can play import UIKit //---------------------- Hello world ---------------------- var str = "Hello, playground" print("Hello world and " + str) print("您好") //---------------------- 数据类型 ---------------------- //整型与浮点型 let age = 24 let pi = 3.14 print(Int.max) print(Int.min) //自动类型推断 var name : String = "fengdianlong" var count : Int = 10 var mount = 10 //类型推断(声明遍历时不用指定类型) //元组 var valueTuple = ("feng",24,"shandongjinan","山东大学威海校区") print(valueTuple.0) print(valueTuple.1) print(valueTuple.2) print(valueTuple.3) var namevalueTuple = (name:"fengdianlong",age:24) print(namevalueTuple.name) //Optional可选类型 var address :String? address = "shandongjinan" //---------------------- 运算符 ---------------------- //算数运算符 print(100/10) print(7%3) print(3*4) print(5+3) //组合赋值运算符 var a = 10 a += 4 print(a) //比较运算符 print(1>1) print(3>=9) //逻辑运算符 print("feng"=="long" || "feng"=="feng") print(true && false) print(true || false) print(!true) //---------------------- 字符串 ---------------------- var str1 = "" var str2 = " " str1.isEmpty str2.isEmpty var wo = "W" //默认为字符串 var me : Character = "m" //指定为字符 //遍历字符串 var words = "这里是我的字符串" for word in words.characters { print(word) } //字符串 + 字符串 let swiftstring1 = "Malcolm " let swiftstring2 = "Feng" var swiftstring = swiftstring1 + swiftstring2 swiftstring.append("hahahah") //字符串插值 let sir = "Malcolm" let type = "G" let number = "11" let price = 1538.6 let message = "\(sir)先生,您订购了\(type)\(number)的往返机票,总价为:\(price * 2)" //---------------------- 数组 ---------------------- let array1 : [Int] = [Int](repeatElement(3, count: 10)) let array2 = Array(1...10) var places = ["beijing","shanghai","wuhan","zhejiang","jiaxing"] places.count places.isEmpty places.append("jinan") places += ["weihai","qingdao","yaoqang"] places[1] places.insert("xuanjie", at: 1) places.remove(at: 2) //places.removeFirst() //places.removeLast() //places.removeAll() places.contains("jinan") places.sort() places.sorted() //---------------------- 字典 ---------------------- var students = ["name":"fengdianlong", "age":"24", "address":"shandongjinan"] students.count students.isEmpty students["address"] = "beijing" //修改 students["name"] for (key,value) in students { print(key,value) } for key in students.keys { print(key) } let keys = [String](students.keys) //提取keys到一个数组 //---------------------- 控制语句 ---------------------- for _ in 1...10{ print("1") } var sum = 0 for i in 1...100{ sum += i } sum var money = false if(money){ print("注孤生") }else{ print("找个女朋友") } var temp = 50 switch temp { case 40...50: print("热死人了") default: print("还活着") } //---------------------- 函数 ---------------------- func addCount(a:Int, b:Int) -> Int{ return a + b } // 调用 var aaa = addCount(a: 7, b: 8) // 多返回值 func maxMin() -> (Int, Int){ return (Int.min, Int.max) } // 默认参数 func function(a:Int, increment:Int = 3) -> Int{ return a + increment } // 函数类型参数 func calculate(x:Int, y:Int, method:(Int,Int) -> Int ) -> Int{ return method(x,y) } func add(x:Int,y:Int) ->Int{ return x + y } func multiply(x:Int,y:Int) ->Int{ return x * y } //---------------------- 枚举 ---------------------- enum Weather{ case sunny case cloudy case froggy case snow case rainy } Weather.sunny