美文网首页
iOS swift字典、元组

iOS swift字典、元组

作者: 我不白先生 | 来源:发表于2021-02-22 15:47 被阅读0次
//隐式
//var dic = ["name":"xiaoming","sName":"xiaoxiaoming","age":18] as [String:Any]
var dic:[String:Any] = ["name":"xiaoming","sName":"xiaoxiaoming","age":18]
//创建可变空字典
var dic2:[String:Any] = [:]
var dic3 = [String:Any]()
//字典遍历
for key in dic.keys {
    print(key)
}
for value in dic.values {
    print(value)
}
//同时获取key和value
for (k,v) in dic {
    print("\(k)=\(v)")
}
//获取数据
var name = dic["name"]
dic.count
//插入数据
dic["x"] = "xx"

//修改数据
dic["x"] = "y"
dic.updateValue("m", forKey: "x")
dic

//元组 类似结构体
var myPoint = (18,20)
var message = (404,"Not Found")
//显式声明
var myPoint3D:(Float,Float,Float) = (1.3,2.4,2.1)
//获取元组中分量的值
myPoint.0
myPoint.1

//给分量命名
var newPoint = (x:18, y:20)
newPoint.x
newPoint.y

var httpResponse = (statusCode:200,message:"Success!")
httpResponse.message
httpResponse.statusCode
httpResponse.statusCode = 404
httpResponse

相关文章

网友评论

      本文标题:iOS swift字典、元组

      本文链接:https://www.haomeiwen.com/subject/wrhxfltx.html