1,当多个不同类型的数据进行操作时,需要转化为形同的数据类型。
let num1 = 1
let num2 = 2.3
var count = num1 + num2
error :
Binary operator '+' cannot be applied to operands of type 'Int' and 'Double'
//正确做法
var count = num1 + Int(num2)
因为swift有类型推导:当swift 知道你输入的类型时,你可以不写数据的类型
Type inference allows you to omit the type when Swift already knows it.
2,循环
while 循环
var aLotOfAs = ""
while aLotOfAs.characters.count < 10 {
aLotOfAs += "a"
}
var repeatCount = 0
repeat {
print("我是最帅的")
repeatCount += 1
} while repeatCount < 100
跳出循环
break
var sum = 1
while true {
sum = sum + (sum + 1)
if sum >= 1000 {
break
} }
终止本次剩余操作,继续执行下一步操作
continue
The continue statement lets you finish the current iteration of a loop and begin the next iteration.
let count = 10
var sum = 0
for i in 1..<count{
if i < 9 {
continue
}
sum += i
print("i = \(i) sum = \(sum)")
/*
i = 9 sum = 9
*/
}
3,Ranges
let count = 10
var sum = 0
for i in 1...count {
sum += i
print("i = \(i) sum = \(sum)")
/*
i = 1 sum = 1
i = 2 sum = 3
i = 3 sum = 6
i = 4 sum = 10
i = 5 sum = 15
i = 6 sum = 21
i = 7 sum = 28
i = 8 sum = 36
i = 9 sum = 45
i = 10 sum = 55
*/
}
let count = 10
var sum = 0
for i in 1..<count {
sum += i
print("i = \(i) sum = \(sum)")
/*
i = 1 sum = 1
i = 2 sum = 3
i = 3 sum = 6
i = 4 sum = 10
i = 5 sum = 15
i = 6 sum = 21
i = 7 sum = 28
i = 8 sum = 36
i = 9 sum = 45
*/
}
let count = 10
var sum = 0
for i in 1...count where i % 2 == 1 {
sum += i
print("i = \(i) sum = \(sum)")
/*
i = 1 sum = 1
i = 3 sum = 4
i = 5 sum = 9
i = 7 sum = 16
i = 9 sum = 25
*/
}
4,Switch
选择语句
let number = 10
switch number {
case 0:
print("Zero")
default:
print("Non-zero")
}
let string = "Dog"
switch string {
case "Cat", "Dog":
print("Animal is a house pet.")
default:
print("Animal is not a house pet.")
}
let hourOfDay = 12
let timeOfDay: String
switch hourOfDay {
case 0...5:
timeOfDay = "Early morning"
case 6...11:
timeOfDay = "Morning"
case 12...16:
timeOfDay = "Afternoon"
case 17...19:
timeOfDay = "Evening"
case 20..<24:
timeOfDay = "Late evening"
default:
timeOfDay = "INVALID HOUR!"
}
let number = 10
switch number {
case _ where number % 2 == 0:
print("Even")
default:
print("Odd")
}
let coordinates = (x: 3, y: 2, z: 5)
switch coordinates {
case (0, 0, 0): // 1
print("Origin")
case (_, 0, 0): // 2
print("On the x-axis.")
case (0, _, 0): // 3
print("On the y-axis.")
case (0, 0, _): // 4
print("On the z-axis.")
default: // 5
print("Somewhere in space")
}
let coordinates1 = (x: 3, y: 9, z: 5)
switch coordinates1 {
case let (x, y, _) where y == x:
print("Along the y = x line.")
case let (x, y, _) where y == x * x:
print("Along the y = x^2 line.")
default:
break
}
5,函数 Function
参数
func printMultipleOf(multiplier: Int, andValue: Int) {
print("\(multiplier) * \(andValue) = \(multiplier * andValue)")
}
printMultipleOf(multiplier: 4, andValue: 2)
func printMultipleOf(multiplier: Int, and value: Int) {
print("\(multiplier) * \(value) = \(multiplier * value)")
}
printMultipleOf(multiplier: 4, and: 2)
func printMultipleOf(_ multiplier: Int, and value: Int) {
print("\(multiplier) * \(value) = \(multiplier * value)")
}
printMultipleOf(4, and: 2)
//默认给value一个值 为1 当不设置value是,value值为1
func printMultipleOf(_ multiplier: Int, _ value: Int = 1) {
print("\(multiplier) * \(value) = \(multiplier * value)")
}
printMultipleOf(4)
** 带有返回类型 **
func multiply(_ number: Int, by multiplier: Int) -> Int {
return number * multiplier
}
let result = multiply(4, by: 2)
函数可以有一个特殊的返回类型:Never,编译器会知道这个函数会一直运行,不会退出。
Functions can have a special Never return type to inform Swift that this function will never exit.
func infiniteLoop() -> Never {
while true {
}
}
直接修改传入参数:inout
一般说来,方法传入的参数是不能改变的。因为传入的参数相当于一个let的常量。
如果你想直接修改传入的参数,就需要inout参数。copy-in copy-out
Parameters are passed as constants, unless you mark them as inout in which case they are copied-in and copied-out.
func incrementAndPrint(_ value: inout Int) {
value += 1
print(value)//6
}
参数类型前面的inout关键字表示该参数应该被复制到方法内,在函数内使用的 copy 副本,然后在函数返回时复制出来。
var value = 5
incrementAndPrint(&value)
print(value)//6
把函数当参数传递
函数可以作为参数传递
func add(_ a: Int, _ b: Int) -> Int {
return a + b
}
func subtract(_ a: Int, _ b: Int) -> Int {
return a - b
}
func printResult(_ function: (Int, Int) -> Int, _ a: Int, _ b: Int) {
let result = function(a, b)
print(result)
}
printResult(add, 7, 2)
printResult(subtract, 7, 2)
网友评论