Swift第一周学习总结

1.程序设计

在这个过程中,在Xcode这个开发软件中,老师通过讲解了几个有趣的运用,比如:牛眼睛,跑人,掷骰子等苹果小运用,让我们感性的认识到了swift语言在IOS开发过程中的重要性,下面就是牛眼睛游戏中比较核心的代码:

@IBAction func hitMeButtonClicked(sender: AnyObject) {
        // lroundf是一个四舍五入的函数
        let yourNum = lroundf(numSlider.value)
        // 通过abs函数得到滑条的值和随机数之间差值的绝对值
        let diff = abs(yourNum - number)
        // 构造一个分支结构用四个分支处理四种不同的情况
        var msg: String         // 给用户的提示信息
        var currentScore: Int   // 当前轮次的成绩
        if diff == 0 {
            msg = "你太厉害了!"
            currentScore = 200
        }
        else if diff < 3 {
            msg = "哎哟, 不错哦!"
            currentScore = 150 - diff
        }
        else if diff <= 5 {
            msg = "你需要多加练习!"
            currentScore = 100 - diff
        }
        else {
            msg = "你是猴子派来的逗逼吗?"
            currentScore = 50 - diff
        }

2.变量和常量

在这一章节中,主要有以下几点:

1、在Swift中规定:在定义一个标识符时必须明确说明该标识符是一个常量还是变量
2、使用let来定义常量,定义之后不可以修改
3、使用var来定义变量,定义之后可以修改
4、常量:let 标识符的名称 : 类型 = 赋值
变量:var 标识符的名称 : 类型 = 赋值
5、类型的首字母要大写,常量与变量名不能包含数学符号,箭头
6、 在开发中,应该优先使用常量,只有发现需要修改时再改成变量,多使用Xcode软件能优化你写的代码。

 定义常量
let a = 9
let b = 0.0
let str : String
 定义变量
var b  = 38
let b = 0.0
let str : String
在一行中定义多个变量
var red,green,brown : String
常量与变量名不能包含数学符号,箭头

其中还介绍了Booleans,只有两种取值,true or false。

let orangesAreOrange = true
let turnipsAreDelicious = false

3.运算符

在这一章节中,运算符主要分为三类:
1,算术运算符: + - * / %

var str1 = "Hello"
var str2 = "world"
str1 = str1 + " " + str2
  1. 逻辑运算符:&& ||
  2. 关系运算符: == != > < >= <=
 var str3 = "Apple"
var str4 = "Abandon"
print(str3 > str4)

4.分支结构

分支结构主要有两种结构:
1.if...else结构:

let diff = salary - insurance - 3500
let rate: Double
let deduction: Double
if diff <= 0{
    rate = 0
    deduction = 0
}
else if diff <= 1500{
    rate = 0.03
    deduction = 0
}
else if diff <= 4500{
    rate = 0.1
    deduction = 105
}
else if diff <= 9000{
    rate = 0
    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

2.switch...case结构

let salary = inputInt() * 13
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 10000001..<100000000:
            print("你是IT大哥,我服")
        default:
            print("我只想说,你是宇宙中的IT大牛")
            }
        }
    else{
        print("你有病!")
    }

5.循环结构

本章循环结构主要有三种:
1.while + 条件 {

}

var i = 1
//while i <= 10{
//    print("Hello,world!")
//    i += 1

循环可能一次都不进行: 1>10
2.repeat{

}while + 条件

var i = 1
repeat{
   i += 1
} while i <= 10

循环至少都能执行一次
3.for (代表变量) in + 条件{

}

for _ in 1...10{
    print("Hello,world")
}

6.数组

1.创建数组

var array1 = [String]()
var array2 : [Int] = []
var array3 = [99,12,34,156,7]
var array4 = [Int](count: 100, repeatedValue: 1)
var array5 = [String]()
var array6 : [String] = []
var array7 = ["apple","rudle","grape"]
var array8 = [String](count: 100, repeatedValue: "")
array3[2...4] = [21,43]
print(array3)

2.获取数组的个数

print(array1.count)
print(array2.count)
print(array3.count)
print(array4.count)
print(array5.count)

3.获取数组中元素进行遍历

for i in 0..

4.向数组中添加元素

array1 += ["strawberry"]
array1.append("pear")
array1.insert("waxberry",atIndex: 0)
array1.insert("watermalon",atIndex: 2)
array1.insert("lemon", atIndex: array1.count)
print(array1)

5.从数组中删除元素

array1.removeAll(keepCapacity: true)//删元素布删空间
array1.removeAtIndex(3)
array1.removeFirst()
array1.removeFirst(<#T##n: Int##Int#>)
/array1.removeAll()
print(array1)

6.数组的复制

var array9 = array3

array9[1] = 20
var array10 = array3[1...3]
print(array9)
print(10)

let array11 = array3 + array9
print(array11)

7.遍历.迭代

for (index,value) in array3.enumerate(){
    print("\(index).\(value)")
}

8.数组的排序

var array = [7,4,5,6,9,1]
let newArray = array.sort()//从小到大
print(array)
let newArray1 = array.sort(>)//大到小
array.sortInPlace()
print(array)
var newarray2 = ("student","teacher","children")
let newArray2 = newArray.sort()
print(newarray2)

1冒泡排序

var array = [21,54,69,45,78]
for i in 0.. array[j + 1]{
            (array[j],array[j + 1]) = (array[j + 1],array[j])
            swapped = true
        }
    }
    if !swapped{
        break
    }
}
print(array)

2简单选择排序

var array = [21,54,69,45,78]
for i in 0..

你可能感兴趣的:(Swift第一周学习总结)