Swift学习之路03-基础,闭包
发布时间:2020-12-14 07:21:53 所属栏目:百科 来源:网络整理
导读:基础变量 let 常量 var 变量 类型标注 var welcomeMessage: String 数值 一个十进制数字,没有前缀 一个二进制数,前缀是 0b 一个八进制数,前缀是 0o 一个十六进制数,前缀是 0x let decimalInteger = 17 let binaryInteger = 0 b10001 let octalInteger = 0 o
基础变量
数值
let decimalInteger = 17
let binaryInteger = 0b10001
let octalInteger = 0o21 // 八进制的17
let hexadecimalInteger = 0x11 // 十六进制的17
元组
let http404Error = (404,"Not Fount")
//你也可以讲一个元组的内容分解(decompose)成单独的常亮
let (statusCode,statusMessage) = http404Error
print("The status code is (statusCode)")
print("The status message is (statusMessage)")
//如果你只需要一部分元组值,分解的时候可以把要忽略的部分用下划线(_)标记:
let (justTheStatusCode,_) = http404Error
print("The status code is (justTheStatusCode)")
//你也可以用下标表示
print("The status code is (http404Error.0)")
// 输出 "The status code is 404"
print("The status message is (http404Error.1)") // 输出 "The status message is Not Found"
//定义元组给每个元素定义名字
let http200Status = (statusCode: 200,description: "OK")
//通过元素名访问元素值
print("The status code is (http200Status.statusCode)")
可选类型
//声明一个变量
var text: String
print(text) //报错因为text没用值所以会抱错
var text: String?
print(text) //打印nil
断言
let age = -3
assert(age >= 3,"A person's age cannot be less than zero") //因为age < 0,所以断言会触发
assert(age >= 3) //断言也可以省略信息
函数
var mathFunction: (Int,Int) -> Int = addTwoInts
print("Result: (mathFunction(2,3))")
func printMathResult(mathFunction: (Int,Int) -> Int,_ a: Int,_ b: Int) { print("Result: (mathFunction(a,b))") } printMathResult(addTwoInts,3,5) // prints "Result: 8"
func stepForward(input: Int) -> Int {
return input + 1
}
func stepBackward(input: Int) -> Int {
return input - 1
}
// 根据传入的bool值来返回指定的函数
func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
return backwards ? stepBackward : stepForward
}
var currentValue = 3
let moveNearerToZero = chooseStepFunction(currentValue > 0)
// moveNearerToZero now refers to the stepBackward() function
func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
func stepForward(input: Int) -> Int { return input + 1 }
func stepBackward(input: Int) -> Int { return input - 1 }
return backwards ? stepBackward : stepForward
}
var currentValue = -4
let moveNearerToZero = chooseStepFunction(currentValue > 0)
// moveNearerToZero now refers to the nested stepForward() function
while currentValue != 0 {
print("(currentValue)... ")
currentValue = moveNearerToZero(currentValue)
}
print("zero!")
// -4...
// -3...
// -2...
// -1...
// zero!
闭包
func compare(a: Int,b: Int) ->Bool {
return a > b
}
var newArray = array.sort(compare)
var newArray = array.sort { (a,b) -> Bool in
return a > b
}
print(newArray.description)
// 我自己写的sort方法
class SwiftClosure {
// 定义一个闭包
static func sort(array: Array<Int>,@noescape function:(a: Int,b: Int) -> Bool) -> Array<Int> {
var newArray: Array<Int>
newArray = []
for i in 0...array.count - 1 {
if newArray.count == 0 {
newArray.append(array[i])
continue
}
for j in 0...newArray.count - 1 {
if function(a:array[i],b: newArray[j]) {
newArray.insert(array[i],atIndex: j)
break
} else {
newArray.append(array[i])
break
}
}
}
return newArray
}
}
// 外部调用
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view,typically from a nib.
var array:[Int] = [5,5,2,6,1]
array = SwiftClosure.sort(array) { (a,b) -> Bool in
return a > b
}
print(array.description)
// print [6,1]
}
func someFunctionWithNoescapeClosure(@noescape closure: () -> Void) { closure() }
typealias 是用来为已经存在的类型重新定义名字的,通过命名,可以使代码变得更加清晰。使用的语法也很简单,使用 typealias 关键字像使用普通的赋值语句一样,可以将某个已经存在的类型赋值为新的名字 import Foundation
// 包含Block的类
typealias funcBlock = (Array<Int>) -> () //或者 () -> Void
class SwiftClosure {
var blockD: funcBlock?
var newArray: Array<Int> = []
// 定义一个闭包
func sort(array: Array<Int>,@noescape function:(a: Int,b: Int) -> Bool,block: funcBlock) -> Void {
newArray = []
for i in 0...array.count - 1 {
if newArray.count == 0 {
newArray.append(array[i])
continue
}
for j in 0...newArray.count - 1 {
if function(a:array[i],b: newArray[j]) {
newArray.insert(array[i],atIndex: j)
break
} else {
newArray.append(array[i])
break
}
}
}
blockD = block
}
func getNewArray()->Void {
blockD!(newArray)
}
}
// 调用含有Block的类
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view,typically from a nib.
let array:[Int] = [5,1]
let swiftClosure = SwiftClosure()
swiftClosure.sort(array,function: { (a,b) -> Bool in
return a > b
}) { (newArray) -> () in
print(newArray)
}
swiftClosure.getNewArray() // 调用这句话后控制台打印[6,5,2,1]
}
}
我日了,写Swift写的不会写OC了!!!WTF我日了,在Xcode中Swift跟OC一比,完全是Xcode不支持Swift啊!!!WTF(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |