Swift 基础语法二

一 数组

数组是一堆有序的由相同类型元素构成的集合类型

定义

let stuArray : Array = ["zhangsan","18"]

获取长度

array.count

判空

array.isEmpty

取值

array[1]

array[0...2]

array[0..<2]

array[..2]

array[2..]

array.first

array.last

array.prefix(2)

array.suffix(2)

添加数据

array.append("zhouliu")

插入数据

array.insert("haojian",at:0)

修改数据

array[0] = "wangqi"

删除数据

remove 会改变原数组的值,返回被删除的那个元素

drop 不会改变原数组的值,返回删除元素后的新数组

array.remove(at:0)
array.removeFirst()
array.removeLast()
array.removeAll

array.dropFirst()
array.dropLast()

是否包含

array.contains("zhangsan")

倒序

array.reverse

索引

var students = ["zhangsan","Lisi","Wangwu","zhangsan"]

students.firstIndex(of :"zhangsan")
students.lastIndex(of :"zhangsan")

合并

var array = ["zhangsan" , "lisi","wangwu"]
var array1 = ["zhaoliu","wangqi"]

var array2 = array + array1

遍历

var array = ["zhangsan" , "lisi" , "wangwu"]

for i in 0..< array.count
print(array[i])
}

for item in array {
print(item)

}

for item in array[0..<2]
{
print(item)
}

for (index,name) in array.enumerated() {
print(index)
print(name)
}

var array = ["zhangsan","lisi","wangwu"]

var i = array.makeIterator()

i.next()
i.next()
i.next()

二 集合

与数组的概念差不多,主要区别:
元素无序
元素不能重复

let stuSet:Set = ["zhangsan","18"]

数组转集合

let stuArray = ["zhangsan","18"]

let stuSet = Set(stuArray)

定义空集合

let set: Set = Set()

let set: Set = []

获取长度

set.count

判空

set.isEmpty

取值

set.first

Set(set.prefix(2))

Set(set.suffix(2))

插入数据

set.insert("man")

三 字典

字典是由键值对 key:value 对组合的集合类型
字典中的元素之间是无序的

var dict1: Dictionary = ["name":"zhangsan","age":"18"]

var dict1: [String : String] = [:]

获取长度

dict.count

判空

dict.isEmpty

查询数据

dict["name"]

修改数据

dict["name"] = "lisi"

删除数据

dict.removeValue(forKey : "height")

合并

var dict1:[String:String] = ["name":"zhangsan","age":"20"]

var dict2:[String:String] = ["height":"1.8","phoneNum":"188888"]

for (key,value) in dict2 {
dict1[key] = value
}

print dict1

遍历

var dict:[String:String] = ["name" : "zhangsan","age":"18"]

for value in dict.values{
print (value)
}

var dict :[String:String] = ["name":"zhangsan","age":"18"]

for key in dict.keys{
print(key)
}

var dict: [String:String] = ["name":"zhangsan","age":"18"]

for (key,value) in dict{
print("(key) - (value)")

}

枚举方式遍历

var dict :[String:String] = ["name":"zhangsan","age":"18"]

for (index,value) in dict.enumerated() {
print("(index) - (value)")
}

迭代器

var dict:[String : String] = ["name":"zhangsan","age":"18"]

var d = dict.makeIterator()

a.next()

a.next()?.value

四 函数

函数是由一组语句构成的代码块,用于完成某个特定的功能呢。

定义与调用

访问修饰符 func 函数名(形式参数列表) -> 返回值类型 {
代码块
return 返回值
}

标签与名称

每个函数的形式参数 都包含 形式参数标签 和形式参数名称

可变参数

for total(numbers: Int...) -> Int{
var sum = 0
for i in numbers{
sum += i
}
return sum

}

total ()

total (numbers : 10)

输入输出参数

func swapInt(a:inout Int ,b: inout Int){
let temp = a
a = b
b = temp
}

var a = 10

var b = 20

SwapInt (a:&a , b:&b)

五 闭包

定义像函数,单没有名字
调用类似函数,也需要传参数
通过查看类型会发现就是前面讲的函数类型
闭包是一种特殊的没有名字的函数

let squareClosure = { (param : Int) -> Int in
return param*param

}

squareClosure(3)

{ (parameters) -> return type in

statements

}

1 来顺序调用闭包的参数

省略 -> 返回类型

省略 参数类型和括号

省略 一条执行语句时 ,return 关键字省略

参数名称缩写 改为 1

捕获

var vehicle = "car"
var animal = "Cat"

let closure = { [vehicle] in

print ("Viehichle is (vechicle)")

print ("Animale is (animal)")

}

vehicle = "Airplane"

animale = "Dog"

closure ()

捕获列表中的 vehicle 是 car 是拷贝类型

animal 是引用类型 ,是 Dog

尾随闭包

闭包是函数的最后一个参数

逃逸闭包

闭包在函数执行完之后再调用

你可能感兴趣的:(Swift 基础语法二)