//: Playground - noun: a place where people can play
import UIKit
//-----------------------一、数组 Array表示数据
//Swift 数组使用【有序列表】存储同一类型的多个值。相同的值可以多次出现在一个数组的不同位置中。
//1定义一个不可变数组
let array = ["red","black","pink"]
//Any任意数据类型
letarray2 = [2,3,4,"str",5,6]as[Any]
//2定义一个可变数组
vararrayMs =Array()
vararrayMs2 = [String]()
vararrayMs3:[String] = []
//3数组中可以存放多种数据类型
letnewArray:[Any] = [100,12,13,"name","image"]
varreArray = [String](repeatElement("aixi", count:10))
//可变数组的基本操作(增删改查)
//4添加元素
arrayMs.append("ball")
print(arrayMs)
arrayMs.append("games")
arrayMs.append("chess")
arrayMs.append("movie")
arrayMs.insert("music", at:1)//插入
//5删除元素
arrayMs.remove(at: 0)
//arrayMs.removeAll()//
//6修改元素
arrayMs[1] = "Demo"
print(arrayMs)
//7根据下标获取元素
arrayMs[0]
//arrayMs[4]//下标越界错误 Index out of range
//8数组的遍历
print("-----forin直接遍历值-------")
fornameinarrayMs{
print(name)
}
print("------通过枚举方法遍历------")
for(index,name)inarrayMs.enumerated() {
print("下标是\(index)对应值是\(name)")
}
print("------通过值域遍历数组中的一部分元素------")
foriinarrayMs[0..<2]{
print(i)
}
//9判断数组中是否包含某个元素
arrayMs.contains("music")
//10数组的合并 注意:数据类型不一样不能合并
letarraya = [10,20,30]
letarrayb = [3,4,5]
//var newArr = array/Ms + arraya
varnewArr =arraya+arrayb
//-----------------------二、字典
//字典的定义Swift 字典用来存储无序的相同类型数据的集合,Swift 字典会强制检测元素的类型,如果类型不同则会报错。
//Swift 字典每个值(value)都关联唯一的键(key),键作为字典中的这个值数据的标识符。
//1定义不可变的字典
letdict :Dictionary= [1:"a",2:"b",3:"c",4:"d"]
let strDict:Dictionary = ["one":"a","two":"b","three":"c"]
letanyDict:[String:String] = ["one":"1"]
//2定义一个可变字典
print("-------------------可变字典 的常用操作------")
var changeDict:Dictionary = ["one":"a","two":"b","three":"c"]
//NSMutableDictionary *dict=[[NSMutableDictionary alloc]init]
//可变字典的操作(增删改查)
changeDict["four"] = "d"
changeDict["five"] = "e"
print("增加后",changeDict)
//删除元素
changeDict.removeValue(forKey: "four")
print("删除后\(changeDict)")
//修改元素
changeDict.updateValue("five", forKey:"five")
print("修改后",changeDict)
//取出元素
changeDict["one"]
//字典的遍历
print("------遍历所有key------")
for key in changeDict.keys{
print(key)
}
print("------遍历所有value------")
vardictArra = [String]()
for value in changeDict.values{
dictArra.append(value)
}
print(dictArra)
print("-----遍历key和对应的值-------")
for(key,value)inchangeDict{
print(key,value)
}
//练习page176
//1、给出下面字典,会执行出什么结果 2
//2、哪一段代码会将一个项目插入到插接器的集合中?2
//-----------------------三、元祖
letyuanzu = ("1001","张三",19,1.88)
let yuanzu2:(String,Int,Double,String,Float) = ("age",2,2.3,"name",1.3)
print("\(yuanzu2.0)")//通过下标获取
yuanzu2.1
yuanzu2.2
varperson = (name:"Tina",age:30,score:100.0)
person.score
person.name
person.age =40
print(person)//通过名称获取
var strnew = "my name is Tom"//倒序排序
//方法一
vartitle2 =""
for i in strnew {
title2=String(i) +title2
}
print("方法一\(title2)")
//方法二
vartitle =""
for i in strnew{
title.insert(i, at:title.startIndex)//n ym
}
print("方法二\(title)")
//方法三
strnew = "my name is Tom"
varnewSt =""
for i in strnew.reversed(){
newSt.append(i)
}
print("方法三\(newSt)")
//方法四
var string="my name is Tom"
vararraystr:[String] = []
for i in string{
arraystr.append(String(i))
}
print("排列后的array is\(arraystr)")
foriin1..
forjin0..
lettemp =arraystr[j]
arraystr[j] =arraystr[j+1]
arraystr[j+1] = temp
}
}
print(arraystr)
varmyname =""
for i in arraystr{
myname = myname + i
}
print("方法四\(myname)")