swift语言创建元组的有几种方式
2个回答
展开全部
importUIKit
//元组的定义
let http404Errot = (404,"Not Found")
letonePerson:(Int ,String,Double) = (001,"Tarena",5000)
var http200Status = (200,"OK")
var point = (x:0,y:0)
//元组的访问
let (status,description) = http404Errot
status
description
let (statusCode,_) = http200Status
statusCode
//通过下标访问
var code = http404Errot.0
var describe = http404Errot.1
http200Status.0 = 201
http200Status.1 = "Unknown"
//通过元素名称访问
println("\(point.x),\(point.y)")
varanotherPerson = (id:002, name:"Guodh", salary:60000.0)
anotherPerson.name = "Guodenghong"
anotherPerson.salary += 20000.0
anotherPerson.0
//元组和switch
letonePoint = (0,1)
switchonePoint {
case (0,0):
println("在原点")
case (_,0):
println("在x轴上")
case(0,_):
println("在y轴上")
case(-2...2,-2...2):
println("在4*4的矩阵内")
default:
println("在4*4的矩阵外")
}
//值绑定
letanotherPoint = (1,0)
switchanotherPoint {
case (let x ,0):
println("点在x轴上,且x的值为\(x)")
case (0,let y):
println("点在y轴上,且y的值为\(y)")
case let(x,y):
println("点不在x轴上也不在y轴上,点的位置为(\(x),\(y))")
}
//使用where关键字增加判断条件
letyetAnotherPoint = (1,2)
switchyetAnotherPoint {
case let(x,y) where x == y:
println("(\(x),\(y))在直线x==y上")
case let(x,y) where x == -y:
println("(\(x),\(y))在直线x==-y上")
case let(x,y):
println("不在两条斜线上")
}
//元组的定义
let http404Errot = (404,"Not Found")
letonePerson:(Int ,String,Double) = (001,"Tarena",5000)
var http200Status = (200,"OK")
var point = (x:0,y:0)
//元组的访问
let (status,description) = http404Errot
status
description
let (statusCode,_) = http200Status
statusCode
//通过下标访问
var code = http404Errot.0
var describe = http404Errot.1
http200Status.0 = 201
http200Status.1 = "Unknown"
//通过元素名称访问
println("\(point.x),\(point.y)")
varanotherPerson = (id:002, name:"Guodh", salary:60000.0)
anotherPerson.name = "Guodenghong"
anotherPerson.salary += 20000.0
anotherPerson.0
//元组和switch
letonePoint = (0,1)
switchonePoint {
case (0,0):
println("在原点")
case (_,0):
println("在x轴上")
case(0,_):
println("在y轴上")
case(-2...2,-2...2):
println("在4*4的矩阵内")
default:
println("在4*4的矩阵外")
}
//值绑定
letanotherPoint = (1,0)
switchanotherPoint {
case (let x ,0):
println("点在x轴上,且x的值为\(x)")
case (0,let y):
println("点在y轴上,且y的值为\(y)")
case let(x,y):
println("点不在x轴上也不在y轴上,点的位置为(\(x),\(y))")
}
//使用where关键字增加判断条件
letyetAnotherPoint = (1,2)
switchyetAnotherPoint {
case let(x,y) where x == y:
println("(\(x),\(y))在直线x==y上")
case let(x,y) where x == -y:
println("(\(x),\(y))在直线x==-y上")
case let(x,y):
println("不在两条斜线上")
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
let yz = (1,2) // 第一种, 访问方式: yz.0 yz.1
let (age,name) = (1,"name") //第二种,访问方式: print("\(age) \(name)")
let user = (name:"name" ,age:14) //第三种,访问方式:print("\(user.name) \(user.1)")
let (par1,par2,par3):(Int,Bool,String) = (1,false,"par3")
// 第四种同第二种相同,只是多了类型的指定,访问方式同第二种一样
print(par1)
print(par2)
print(par3)
目前我就知道这几种方式
let (age,name) = (1,"name") //第二种,访问方式: print("\(age) \(name)")
let user = (name:"name" ,age:14) //第三种,访问方式:print("\(user.name) \(user.1)")
let (par1,par2,par3):(Int,Bool,String) = (1,false,"par3")
// 第四种同第二种相同,只是多了类型的指定,访问方式同第二种一样
print(par1)
print(par2)
print(par3)
目前我就知道这几种方式
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询