前言
当大电脑回归之后我再去尝试了一下Swift for windows
是否可以下载,挂好梯子后,这次竟然速度稳定下来了,下了十多分钟完成下载
下下来是一个窗口化编译器
需要你在IDE里面将代码书写好,然后保存到本地文件,然后再通过编译器定位文件编译以及执行
话不多说,直接开始敲代码实践咯
①输出
exp1
依次键入下列代码测试输出
var index:Int = 100
var indexD:Double = 3.1415
let a = 6
print("a is \(a)")
print(a)
print(index)
print(2.52)
print(indexD)
print("\(index) and \(indexD)")
print("left str"+" and right str")
产生结果
exp2
输入下列代码
var test:Int = 31
print("the result is:"+test)
var testS:String ="31"
print("the result is:"+testS)
编译后你会发现两个代码都报错了
-
第一个代码报错的原因是因为输出中,“+”不能用于连接
String
和Int
类型,报错内容如下
error: binary operator '+' cannot be applied to operands of type 'String' and 'Int' print("the result is:"+test)
-
第二个代码的错误出在定义中,看过我写的Swift学习笔记都能知道,我特地在标题中写了玄学两个字,这个地方报错仅仅是因为等号左边有空格,而右边没有
error: '=' must have consistent whitespace on both sides
改正过后代码如下(同时修改两处错误合成一坨代码)
var test:String = "31"
print("the result is:"+test)
产生结果
exp3
键入下列代码
var left = "this is a pen"
var right = "this is a apple"
print(left+right)
编译后得到运行结果
结论
- 在输出中可以用
+
连接两个字符串进行输出,也可以连接两个字符串变量,也可以连接一个字符串与一个字符串变量,但是无法连接一个字符串与整形变量,强制连接将会报错,无法通过编译 - 变量可以单独进行输出
- 变量如果要融入字符串中的话,需要以
\(variable)
的形式插入到字符串中,比如print("\(index) and \(indexD)")
②if 分支
exp1
键入如下代码,将a的初值设置为3
let a = 3
if(a==3)
{
print("the value of a is 3")
}
else {
print("not 3! the value of a is \(a)")
}
结果
如果将a的初值换为6的话,结果如下
exp2
现在将上述代码中if后面的大括号去掉(尝试缩短代码行数)
if(a==3)
print("the value of a is 3")
然而报错,编译不通过
显示错误
error: expected '{' after 'if' condition
因此可以看出在Swift中if
后面是一定要跟大括号的,哪怕只有一条语句,所以就别省事了,都加上吧
结论
if-else分支中别图省事,大括号全带上,哪怕只有一条语句
③switch语句
exp1
输入如下代码
let x = 3
switch(x)
{
case 3:
print("the value of x is 3")
case 5:
print("the value of x is five")
case 7:
print("the value of x is qi")
default:
print("input error")
}
很多人会说了,你switch没加break语句,你忘了这是Swift吗、、Swift中的switch语句和C中switch不一样,满足条件后并不会往下坠,除非你加了fallthrough
关键字
编译后输出结果
exp2
将每个case语句之后添加fallthrough关键字,再执行
switch(x)
{
case 3:
print("the value of x is 3")
fallthrough
case 5:
print("the value of x is five")
fallthrough
case 7:
print("the value of x is qi")
fallthrough
default:
print("input error")
}
执行结果
尽管只满足了case 3
,但是由于fallthrough
关键字的缘故,会将向下的所有语句都执行
结论
Swift语句和C语句中的switch相反,Swift语句中需要添加fallthrough
来进行下坠,而如果啥都不添加则默认没有下坠
④数组与循环
exp1
输入下列代码初始化一个自定义数组和一个重复元素数组
并且循环输出数组元素
var arrayB:[Int]=[1,2,4,45,6,73,3]
var arrayC=[Int](repeating:1,count:10)
print("what's in arrayB")
for i in arrayB{
print(i)
}
print("what's in arrayC")
for i in arrayC{
print(i)
}
输出结果
同样值得注意的是,和if语句一样,每个for语句都需要一个大括号,不能省略
exp2
向数组中添加元素,合并两个数组
var arrayA:[Int]=[]
var arrayB:[Int]=[1,2,4,45,6,73,3]
print("what's in arrayB")
for i in arrayB{
print(i)
}
//向arrayB中添加 整数 666
arrayB.append(666)
print("what's in arrayB")
for i in arrayB{
print(i)
}
var arrayD:[Int]=[-1,-2,-53]
//合并arrayB 与 arrayD
var arrayC=arrayB+arrayD
print("what's in arrayC")
for i in arrayC{
print(i)
}
exp3
输出数组的长度
print("the length of arrayB is\(arrayB.count)")
print("the length of arrayD is\(arrayD.count)")
print("the length of arrayC is\(arrayC.count)")
输出结果
判断数组是否为空
var arrayA:[Int]=[]
print("arrayA is empty?:\(arrayA.isEmpty)")
print("arrayC is empty?:\(arrayC.isEmpty)")
输出结果
⑤字典
exp1
以下代码涉及字典的建立,遍历输出,输出字典长度,从字典中分解出key和value并遍历输出
var firstDict:[Int:String]=[1:"car",2:"sheep",3:"code"]
for(key,value) in firstDict{
print("\(key) : \(value)")
}
print("firstDict's length:\(firstDict.count)")
//从字典中分理出key和value
let Dictkeys = [Int](firstDict.keys)
let Dictvalue = [String](firstDict.values)
print("dict's key")
for i in Dictkeys{
print(i)
}
print("dict's value")
for i in Dictvalue{
print(i)
}
输出结果
exp2
通过key取出字典里的对应value
var firstDict:[Int:String]=[1:"car",2:"sheep",3:"code"]
var secondDict:[String:Double]=["Chinese":124,"English":131,"Math":140]
print(firstDict[2])
print(secondDict["Math"])
输出结果
不过值得一提的是执行上述代码后会产生如下警告
F:\Swift\firstSwift.swift:8:7: warning: expression implicitly coerced from 'Double?' to Any
print(secondDict["Math"])
^~~~~~~~~~~~~~~~~~
F:\Swift\firstSwift.swift:8:17: note: provide a default value to avoid this warning
print(secondDict["Math"])
~~~~~~~~~~^~~~~~~~
?? <#default value#>
F:\Swift\firstSwift.swift:8:17: note: force-unwrap the value to avoid this warning
print(secondDict["Math"])
~~~~~~~~~~^~~~~~~~
!
F:\Swift\firstSwift.swift:8:17: note: explicitly cast to Any with 'as Any' to silence this warning
print(secondDict["Math"])
~~~~~~~~~~^~~~~~~~
as Any
Q.E.D.
Comments | 1 条评论