swift4基础学习(2)- 循环、元组、条件语句

import UIKit

var str = "Hello, playground"

//1.for循环
//1.1 index 的声明周期只存在for循环内
//...指闭区间 1~10
for index in 1...10{
    print("num=\(index)")
}

//1.2开区间1-4
for index in 1..<5 {
    print("kai=\(index)")
}

//1.3 使用 _ 忽略索引
for _ in 1...5 {
    
}

//1.4 遍历数组
let names = ["小明", "小方" , "李四"]
for name in names where name == "小方"{   //where与sql中的where中用法类似
    print(name)
}

//1.5带有索引的数组遍历
for (index,name) in names.enumerated() {
    
    print("index=\(index): name=\(name)")
}

//2 元组
//定义:使用()并以 “,” 区分,多个值组合而成的复合组。
//元组中的值可以是任意类型,每一个元素的类型可以是不同的
let httpError = (404, "error network")

//2.1获取元组中的值
print(httpError.0,httpError.1)

//2.2元组赋值
let (statusCode,statusDes) = httpError

//2.3忽略元组中的某个值
let (statusCodeTwo,_) = httpError

//2.4元组中的元素定义名字
let http200Status = (status: 200, des: "sucess")
print(http200Status.status,http200Status.des)


//3.1字典遍历
let vegetables = ["鸡蛋": 1, "黄瓜": 4, "番茄": 2]

for (key,value) in vegetables {
    print(key,value)
}
//字典元素遍历顺序和插入顺序可能不同,因为字典内容在内部是无序的,所有遍历元素不能保证顺序


//3.2字符串中的字符也是可以
for char in "hello swift"{
    print(char)
}

//3.3数组  倒叙
for name in names.reversed(){
    print(name)
}

//4.while循环
var count = 0
//第一种
while count < 5 {
    count = count + 1
    print(count)
}

//第二种
repeat{
    count = count + 1
    print("浏览数: \(count)")
}while count < 10


let numberOfTag = [
    "vivo":[24,42,12,49,10],
    "小米":[21,25,12,9,30],
    "华为":[18,43,29,19,10],
]

var lastBig = 0
var valuekey = ""
var valueIndex = 0
for (key,values) in numberOfTag{
    
    //带有索引的数组遍历
    for (index, number) in values.enumerated() {
        if number > lastBig {
            lastBig = number
            valuekey = key
            valueIndex = index
        }
    }
}

print("最大值: \(lastBig) 位置:\(valuekey) \(valueIndex)")


//5. 条件语句
// 在 if语句中,条件必须是一个布尔值表达式,不会隐式的与0做对比

var testC = true

if testC {
    print("YES")
}

//由于if中的必须是bool表达式,那么有两种方式判断一个对象是否为空
var testCondition:String? = "2"

//testCondition 必须声明optional
//如果有值就赋值给condition
// 使用 if 和 let 来处理值缺失情况, 如果变量的值为nil,条件会判断为 false
//oc中 对象为nil时直接为false
if let condition = testCondition {
    print("有值:\(condition)")
}else{
    print("没有值:")
}

//第二种
if testCondition != nil {
    print("testCondition有值")
}

//6. switch
// switch  与oc相比不需要写break
// case 执行完,就会终止switch预计
//case 从一个case与领一个case关联使用fallthrough
//每一个case 分支必须包含一条语句

let characterTest = "a"

switch characterTest {
case "a":
    print("值为a")
    print("------")
    fallthrough
case "b","B":            // 可以包含多个模式,用逗号把他们分开
    print("值为b")
     print("------")
case "c":
    print("值为c")
     print("------")
default:
    print("未知")
}

//区间匹配
let num = 1000
var countType:String?
switch num {
case 1...9:
    countType = "ge"
case 10...99:
    countType = "时"
case 100...999:
    countType = "百"
case 1000...9999:
    countType = "千"
default:
    countType = "未知"
}
print(countType!)

//7.1元组值可忽略
let point = (1,1)

switch point {
case (0,0):
    print("原点")
case (0,_):
    print("y轴")
case (_,0):
    print("x轴")
case (-2...2, -2...2):
    print("在以原点为0,0,边长2根号2的正方形中")
default:
    print("不在轴上")
}

//7.2值绑定
switch point {
case (let x,0):
    print("x点 \(x)")
case (0, let y):
    print("y轴 \(y)")

case (let x, let y):
    print(" \(x) \(y)")

}

//7.3case可以使用 where 语句来判断额外的套件

let anotherPoint = (2,2)

switch anotherPoint {
case let(x,y) where x == y:
    print("位置对称")
case let(x,y) where x == -y:
    print("反对称")
default:
     print("没有对称")
}

猜你喜欢

转载自blog.csdn.net/a1034386099/article/details/88843815