1. swift占位符
类似于oc的%占位,使用(表达式)的形式
let mystr:String="this is a good position"
var myvar:String="hello swift"
let message="\(mystr) sounds good,\(myvar) is studing"
print(message)
输出结果
this is a good position sounds good,hello swift is studing
1. 1 swift占位符
import Foundation
let mystr:String="hello world"
var zhangsan:String="zhangsan",lisi:String="lisi"
print(zhangsan,mystr,lisi, separator:"---")
输出结果
zhangsan—hello world—lisi
2.lnt类型的赋值
这里可以类型推断
swift的数据类型首字母都是大写的
var a=4
var b:Int=9
print("\(a) +\(b)=\(a+b)")
输出结果
4 +9=13
3.类型强制转换
不能作用于不同类型的数据,因此必须转换
如下代码会导致报错,因为运算符两段类型不匹配,因此需要强转
var a:Double=34.32
var b:Float=342.1
var c:Double
c=a*b
改成
var a:Double=34.32
var b:Float=342.1
var c:Double
c=a*Double(b)
print(c)
3.可选及可选绑定
import Foundation
var mystr:String?="lisi"//将mystr定义为可选类型
var str:String!="zhagnsanfeng"//将str进行可选绑定
print(mystr,str!)
结果
Optional(“lisi”) zhagnsanfeng
4.半闭区间和全闭区间
for i in 1...5{
print(i)
}
for i in 1..<5{
print(i)
}
5.1声明及分解元祖
import Foundation
//var yuanzu=("key1":"value1","zhangsan":"张三")
var yuanzu=("zhangsan","male",23)
var (name,gender,age)=yuanzu
print("name is \(name) and gender is \(gender) and age is \(age)")
结果
name is zhangsan and gender is male and age is 23
5.1声明及分解元祖
分解元祖是对声明元祖的取反
let myyuanzu1=("PEK","shoudu airport")
let myyuanzu2=(code:"bookname",value:"iosjington")
print(myyuanzu1.1)
print(myyuanzu2.code)
print(myyuanzu2.value)
分解元祖后,元祖内部实际上都是元祖字典话的键
let (code1,airport1)=myyuanzu1
print(code1)
print(airport1)
输出结果:
shoudu airport
bookname
iosjington
PEK
shoudu airport
5.数组的声明
这里由于是数组,对类型的声明要放在[]中即myarr:[String]
var myarr:[String]=["jiangxi","beijing","fujian"]
for str in myarr{
print(str)
}
以上快速for in语句对遍历元素的类型声明采用了隐式的方式,可以强制加上类型声明
for str:String in myarr{
print(str)
}
6.for in快速遍历字典
let mydic=[“PEK”:“shoudu airport”,“SHA”:“shanghai airport”]//字典类型mydic不能强制指定类型
for (code,airport) in mydic{
print("\(code): \(airport)")
}
输出结果
PEK: shoudu airport
SHA: shanghai airport
7.repeat while 取代do while
do while已经改为repeat while
var a:Int=10
repeat{
print("\(a) is small than 100")
a+=1
}
while (a<100)
8.swith匹配元祖
swith在swift中似乎可以匹配一切
let i=("first","second")
switch i {
case ("first","second"):
print("hello world")
case ("third","fourth"):
print("hello swift")
default:
break
}
9.swith匹配元祖及值绑定
let i=(3,5)
switch i {
case (0,0):
print("point is on zero point")
case (_,0):
print("point is on x-zhou")
case (0,let y):
print("(0,\(y)) point is on y-zhou")
case (0...5,0...5):
print("\(i.0),\(i.1) point is on right up eara")
default:
break
}
10.swith匹配元祖之使用where 子句
let SomePoint = (0,0)
switch SomePoint {
case let (x,y) where x==0 && y==0:
print("\(x),\(y) is on zero point ")
default:
break
}
11.swith控制语句
import Foundation
var temp:String="奇怪的字符"
var yuanzu=(x:6,y:30)
switch yuanzu {
case (0,0):
print("坐落在原点上")
case (_,0)://_匹配所有可能值
print("坐落在x轴上")
case (0,_)://_匹配所有可能值
print("坐落在y轴上")
case (Int.min..<0, 0..<Int.max):
print("着落在第一象限")
case let temp:
print("\(temp) 是不合法的")
default://这个default没有机会执行
print("这是一个默认的答案")
}
输出
(x: 6, y: 30) 是不合法的
这个temp不会去显示为之前定义的常量,而是会接受swith判断条件中的yuanzu变量
12.swith匹配元祖之使用where 子句
import Foundation
var temp:String="奇怪的字符"
var yuanzu=(x:-51,y:54)
switch yuanzu {
case (var x,0) where x==0:
print("坐落在原点上")
case (_,0)://_匹配所有可能值
print("坐落在x轴上")
case (var x,_) where x==0://_匹配所有可能值
print("坐落在y轴上")
case (var x, var y) where x<0 && y>0:
print("着落在左上象限")
case let temp:
print("\(temp) 是不合法的")
default:
print("这是一个默认的答案")
}
效果
着落在左上象限
13.for in循环及不换行打印
import Foundation
for i in 1..<10{
for j in 1...i{
print("\(j)*\(i)=\(i*j) ",terminator:"")//teminator:""是不换行输出
}
print("\n")
}
1*1=1
12=2 22=4
13=3 23=6 3*3=9
14=4 24=8 34=12 44=16
15=5 25=10 35=15 45=20 5*5=25
16=6 26=12 36=18 46=24 56=30 66=36
17=7 27=14 37=21 47=28 57=35 67=42 7*7=49
18=8 28=16 38=24 48=32 58=40 68=48 78=56 88=64
19=9 29=18 39=27 49=36 59=45 69=54 79=63 89=72 9*9=81