swift第一周学习总结
swift 语言是苹果公司在2014年的WWDC大会上发布的全新的编程语言。Swift语言继承了C语言以及Objective-C的特性,且克服了C语言的兼容性问题。Swift语言采用安全编程模式,且引入了多种新功能,使得编程工作更加简便,灵活!
变量就是在后续的写入过程中可以改变的。
```
var myVariable = 42
myVariable = 50
let myConstant = 4
```
常量就是在以后始终还是你最初给它定的值
let constA = 42
print(constA)
let constB:Float = 3.14159
print(constB)
字面量就是能够直接了当地指出自己的类型并为变量进行赋值的值,在swift中我们通常用到整型字面量、浮点型字面量、布尔型字面量
值得注意的是我们通常在定义一个变量和常量时通常就使用int和Double\布尔型(Bool)通常和分支结构连用
let aNumber = 3 //整型字面量
let aString = "Hello" //字符串字面量
let aBool = true //布尔值字面量
字母(Unicode字符)、数字、下划线(数字不能开头)
大小写敏感
不能使用关键字做标识符
驼峰标识 - 第一个单词全小写以后每个单词首字母大写
见名知意
1.算术运算符( + — * / % 等)
2.比较运算符(> < = >= <= !=)
3.短路运算符(&& || !)
4.位运算符
5.赋值运算符
6.范围操作符
7.其它运算符
通过一周的学习,我们学习了swift中的几个条件语句
1.if语句
if a > b {
print("a比b大!!")
}
2.if else语句
let x = inputDouble()
let y: Double
if x < -1 {
y = 2 * x - 5
}
else if x <= 1 {
y = 3 * x + 7
}
print("f(\(x)) = \(y)")
3.if…else if…else语句
print("请输入工资: ", terminator: "")
let salary = inputDouble()
print("请输入五险一金: ", terminator: "")
let insurance = inputDouble()
let diff = salary - insurance
var rate = 0.0
var deduction = 0.0
if diff <= 0 {
}
else if diff <= 1500 {
rate = 0.03
}
else if diff <= 4500 {
rate = 0.1
deduction = 105
}
else if diff <= 9000 {
rate = 0.2
deduction = 555
}
else if diff <= 35000 {
rate = 0.25
deduction = 1005
}
else if diff <= 55000 {
rate = 0.3
deduction = 2755
}
else if diff <= 80000 {
rate = 0.35
deduction = 5505
}
else {
rate = 0.45
deduction = 13505
}
let tax = diff * rate - deduction
print("个人所得税为: ¥\(tax)元")
print("实际到手收入: ¥\(salary - insurance - tax)元")
4.内嵌if语句
// 完美数
let start = NSDate()
for i in 2...100000 {
var sum = 1
var j = 2
while j <= Int(sqrt(Double(i))) {
if i % j == 0 {
sum += j
if i / j != j{
sum += i / j
}
}
j += 1
}
if sum == i {
print(i)
}
}
5.switch语句
// IT从业人员等级评定
print("请输入你的月薪: ", terminator: "")
let salary = inputInt()
if salary >= 0 {
switch salary {
case 0:
print("你是一个无业游民")
case 1...30000:
print("你是一个小码畜")
case 30001...60000:
print("你是一个小码奴")
case 60001...100000:
print("你是一个大码农")
case 100001...200000:
print("你是一个光荣的IT民工")
case 200001...500000:
print("你是一个优秀的IT工程师")
case 500001..<10000000:
print("你是IT精英")
case 10000000..<100000000:
print("你是IT大哥, 我服")
default:
print("你是头上有光环的IT领袖, 求带")
}
}
else {
print("你丫有病!!!")
}
我们需要多次执行同一块代码。一般情况下,语句是按顺序执行的:函数中的第一个语句先执行,接着是第二个语句,依此类推
我们学习了几种循环类型
1.for-in循环
var sum = 0
for i in 1...100 {
sum += i
}
print(sum)
2.while循环
var i = 1
while i <= 100 {
sum += i
i += 1
}
print(sum)
3.repeat…while循环
var i = 1
repeat {
print("\(i). Hello, world!")
i += 1
} while i <= 100
所谓数组就是用来装一类东西的集合
1.创建数组
var array1 = [Int]()
var array2: [Int] = []
var array3 = [99,12,34,156,7]
var array4 = [Int](count: 100, repeatedValue: 1)
var array5 = [String](count: 10, repeatedValue: " ")
var array6 = ["hello","world","enheng","babaey"]
var array7: [String] = []
var array8 = [String]()
2.获取数组中的元素
print(array1.count)
print(array2.count)
print(array3)
print(array6.count)
3.向数组中添加元素
array1.append(999)//追加
array1.append(888)
array1.insert(777, atIndex: 0)//插入
array1.insert(666, atIndex: 2)
array1.insert(555, atIndex: array1.count)
print(array1)
4.删除元素
array6.removeFirst()
print(array6)
5.数组复制
var array9 = array3
array9[0] = 333
print(array9)
print(array3)
var array10 = array3[1...3]
print(array10)
6.数组相加
var array11 = array3 + array10
print(array11)
用一个变量或者一个常量保存多项数据数据信息的类型
let stu: (id: Int, name: String, gender: Bool, age: Int) = (1001, "王大锤", true, 23)