import UIKit
字符串
let name: String = "zhangdanfeng"
let name01 = "zhangdanfeng01"
for char in name.characters{
print(char)
}
let names = name + name01
let age = 30
let info = "name:\(name), age:\(age)"
let min = 2
let second = 18
let timeStr = String(format: "%02d:%02d", min, second)
let timeStr01 = String(format: "%02d:%02d", arguments: [min, second])
let urlStr = "www.baidu.com"
//let subStr = urlStr.substring(to: String.Index)//Index类型比较麻烦,所以可以把String转换成NSString进行字符串的截取
let subStr = (urlStr as NSString).substring(to: 3)
let subStr01 = (urlStr as NSString).substring(with: NSMakeRange(4, 5))
let subStr02 = (urlStr as NSString).substring(with: NSMakeRange(10, 3))
数组
1,数组定义
- 不可变数组
//只放字符串的不可变数组
let array: [String] = ["zhang","dan","feng"]//最常用
let array01: Array = ["zhang","dan","feng"]
//可以放任意类型的不可变数组
let array02 = ["zhang", "dan", "feng",20] as Any
let array03: Any = ["zhang", "dan", "feng",20] as Any
- 可变数组
//只放字符串的可变数组
var arrayMutable = Array()
var arrayMutable01 = [String]()
//可以放任意类型的可变数组
var arrayMutable02 = Array()
var arrayMutable03 = [Any]()
2,可变数组的基本操作
- 添加
arrayMutable.append("zhangdanfeng")
arrayMutable.append("li")
arrayMutable.append("d")
- 删除
arrayMutable.removeLast()
arrayMutable.remove(at: 2)
- 修改
arrayMutable[1] = "OK"
- 查询元素
arrayMutable[1]
let myStr = arrayMutable[1]
3,遍历数组
//常用
for var i in 0..
4,数组合并(相同类型的数组才可以合并)
let combinedArray = array + array01
//let combinedArray01 = array02 + array03//两个Any数组也不能合并
字典
1,定义字典:字典也是用中括号【】
- 不可变字典
let dict:Dictionary = ["name":"zhangdanfeng", "age":"20"];
let dict01 = ["name":"zhangdanfeng", "age":"20"];
//let dict02:Dictionary = ["name":"zhangdanfeng", "age":"20"];//一般这种写法会用下面那一句
let dict02 : [String: Any] = ["name":"zhangdanfeng", "age":"20"];
let dict03 = ["name":"zhangdanfeng", "age":20] as Any;
- 可变字典
var dictMutable = Dictionary()
var dictMutable01 = [String:Any]()
var dictMutable02 = [String:Any]()
var dictMutable03 = [String:AnyObject]()
2,对可变字典的基本操作
- 添加元素
dictMutable["name"] = "zhangdanfeng" as NSObject?
dictMutable["age"] = 20 as NSObject?//这是因为swift中的字符串不是NSObject类型,不是NSString,而是String类型
dictMutable["num"] = 55 as NSObject?
dictMutable
- 删除元素
dictMutable.removeValue(forKey: "name")
dictMutable
- 修改元素
dictMutable["age"] = 24 as NSObject?
dictMutable
- 获取元素
let age = dictMutable["age"]
3,遍历字典
for key in dictMutable.keys{
print(key)//注意是无序的
}
for value in dictMutable.values {
print(value)
}
for (key, value) in dictMutable {
print("key:\(key), value:\(value)")
}
4,合并字典
//即使类型一致也不能直接相加合并,想合并只能遍历添加合并
var myDict01 = ["name":"zhangdanfeng", "age":24, "num":100] as [String : Any]
var myDict02 = ["height":1.69];
for (key, value) in myDict02{
print(key, value)
myDict01[key] = value
}
元组
元组:一般可以用于作为方法的返回值(之后还会提到)
为什么用元祖:
从下面可以看到,如果字典或者数组保存的类型是any的时候,取出的类型也是any,那么在使用的时候需要转换,甚至有时候强制转换出错的时候就会造成程序崩溃
元祖的优点是取出值的类型就是其保存的类型,而且在取出的时候还会有提示
//先看一下数组如何保存
let array = ["zhangdanfeng", 18, 1.69] as [Any]//一般我们也不会这么写,而是用下面这句
let array: [Any] = ["zhangdanfeng", 18, 1.69]
array[0]//因为存储的时候是any,所以取出的也是any类型,需要转换
//然后是字典
let dict : [String : Any]= ["name":"zhangdanfeng", "age":24]
dict["name"]//因为存储的时候是any,所以取出的也是any类型,需要转换
//方式一:常规方式
let info = ("zhangdanfeng", 18, 1.69)
info.0//String类型
info.1//Int类型
info.1//Double类型
- 可以给元组起别名
//方式二:类zi'dian
let info01 = (name:"zhangdanfeng",age:20)
info01.name
info01.0
let (name, age, height) = ("zhang", 20, 2)
name
age
height