首先谈点开心的:今天是周二,广州的天气格外明朗,早上上班的心情也不一样,最值得高兴事,很快到五一劳动节了,说到劳动节,放假是吧。你懂的。再来谈谈我上周的工作总结,上周可以说是黑轮压城城欲摧,甲光向日金鳞开,上周公司计划在一周之内完成项目上线,可能会有人问,一周之内怎么可能完成一个项目,呵呵,一周内理论而言不可能完成一个项目,我们只是在老板的要求下做出一个半成品来,发布上线,看看用户对我们的软件的反馈和体验方面的建议。希望用户能给我们更多的建议,我们才能将我们的产品做得更优,更至。回过头来再来说说我自己吧,自己也很幸苦,加班加到九点,周六也不休息,晚上回去自己优化自己的代码,同时也学习一些新的技术,压力是必不可少的。好了不扯这些了,我们来学习swift这门高级语言吧。
1.打开你的xcode,新建一个playgound文件
(1)输出语句;
var varible = "hello,playground" print(varible) print("hello,playGround");
注意:在swift语言中,加;和不加都表示一个独立的语句。
(2)常量与变量:常量用let来修饰 ,变量用var来修饰
let const = 40 let name = "这是常量"; let width = 76.0; var varii = 45;
(3)如果常量或变量没有初始值或者初始值的信息不明确,需要指定类型
var age:Float = 23 let myContst:String = "dddddd"
(4)类型的转换
(4.1)
let apples = 5; let pears = 7; let str = "I have (apples) apple and (pears) pear"
(4.2)
let apples = 5; let pears = 7; let str = String(apples)+String(pears);
(5)使用[]来创建数组和字典,并通过下标和key来访问元素
(5.1)数组
var goodList = ["shoes","coats","towers","candy","trousers"] print(goodList[1]); goodList[0]="shoe" print(goodList)
(5.2)字典
var dic = [ "name":"zhangsan", "age":23, "sexal":"男", ] print(dic["name"]) dic["age"] = 24
(6)控制流:使用if和switch来进行条件操作,使用for-in、for、while和do-while来进行循环。包裹条件和循环变量括号可以省略,但是语句体的大括号是必须的。
(6.1)for in 控制流
let indivadulScore = [70,66,80,90,56,34] var score = 0 for s in indivadulScore { if s>60 { score += 20 }else { score += 100 } }
注意:看看和你之前写的控制流语法上有哪些区别
(6.2)?表示可选的
var optionString:String? = "hello,world" optionString == nil; print(optionString) var optionName:String? = "welcome,please" var ogreeting = "hello" if let name = optionString { ogreeting = "hello,(optionName)" }
(6.3)分支开关语句
let cons = "red paper" switch cons { case "red paper": let vegetableComment = "Add some raisins and make ants on a log." case "white paper": let vegetableComment = "I do not want to eat" default: let vegetableComment = "Everything tastes good in soup." }
(6.4)for in遍历数组
let numberArr = [ "zuqiu":[2,5,7,9,4,2,1], "yumaoqiu":[1,2,3,4,5,6,7], "lanqiu":[1,5,6,4,3,6,8], "taiqiu":[34,51,12,3,5,6,9], ] var Interest = 0 for (kind,numbers)in numberArr { for number in numbers { if number < Interest { Interest = number; } } }
(6.5)使用while来重复运行一段代码直到不满足条件。循环条件可以在开头也可以在结尾。
var n = 2 while n < 100 { n = n * 2 }
(6.6)do {}while 只需将do 改为repeat
var m = 2; repeat { m = m * 2 }while m < 100 m
(6.7)可以循环中使用..来表示范围
var secondForLoop = 0 for i in 0 ..< 3 { secondForLoop += 1 } secondForLoop