())? /* 閉包的..."/>
您好,登錄后才能下訂單哦!
import UIKit
class HttpTool: NSObject {
var callBack : (()->())?
/*
閉包的寫法:
類型:(參數(shù)列表) -> (返回值)
建議:寫閉包時(shí),記住格式直接先寫 () -> ()
在需要參數(shù)或者返回值,在內(nèi)部填充對(duì)應(yīng)的東西即可
*/
func loadData(callBack : () -> ()) {
self.callBack = callBack
dispatch_async(dispatch_get_global_queue(0, 0)) {
print("網(wǎng)絡(luò)請(qǐng)求數(shù)據(jù):", NSThread.currentThread())
dispatch_async(dispatch_get_main_queue(), {
callBack()
})
}
}
}
import UIKit
class ViewController: UIViewController {
// 使用類時(shí)不需要導(dǎo)入類,默認(rèn)自己創(chuàng)建的類在同一個(gè)命名空間中
var httpTool : HttpTool = HttpTool()
override func viewDidLoad() {
super.viewDidLoad()
}
func btnClick() {
print("btnClick")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// 閉包的常規(guī)寫法
// httpTool.loadData { () -> () in
// print("加載數(shù)據(jù)完成,更新界面:", NSThread.currentThread())
// }
// 閉包的簡(jiǎn)寫:如果閉包是沒有參數(shù),并且沒有返回值,那么閉包中 () -> () in 可以省略
// 開發(fā)中建議該寫法
// 解決方案一:
// weak var weakSelf = self
//
// httpTool.loadData {
// print("加載數(shù)據(jù)完成,更新界面:", NSThread.currentThread())
// weakSelf!.view.backgroundColor = UIColor.redColor()
// }
// 解決方案二:
// weak var weakSelf = self
// httpTool.loadData {[weak self] () -> () in
// print("加載數(shù)據(jù)完成,更新界面:", NSThread.currentThread())
// self!.view.backgroundColor = UIColor.redColor()
// }
// 解決方案三:
// unowned關(guān)鍵字相當(dāng)于__unsafe_unretained,指向一個(gè)內(nèi)存地址,不管該對(duì)象是否被銷毀.依然指向該內(nèi)存地址
httpTool.loadData {[unowned self] () -> () in
print("加載數(shù)據(jù)完成,更新界面:", NSThread.currentThread())
self.view.backgroundColor = UIColor.redColor()
}
}
// 析構(gòu)函數(shù)(相當(dāng)于OC中dealloc方法)
deinit {
print("ViewController----deinit")
}
}
第二: 閉包簡(jiǎn)介
/*
閉包:
函數(shù)是閉包的一種
類似于OC語(yǔ)言的block
閉包表達(dá)式(匿名函數(shù)) -- 能夠捕獲上下文中的值
語(yǔ)法: in關(guān)鍵字的目的是便于區(qū)分返回值和執(zhí)行語(yǔ)句
閉包表達(dá)式的類型和函數(shù)的類型一樣, 是參數(shù)加上返回值, 也就是in之前的部分
{
(參數(shù)) -> 返回值類型 in
執(zhí)行語(yǔ)句
}
*/
// 完整寫法
let say:(String) -> Void = {
(name: String) -> Void in
print("hi \(name)")
}
say("lnj")
// 沒有返回值寫法
let say2:(String) ->Void = {
(name: String) in
print("hi \(name)")
}
say2("lnj")
// 沒有參數(shù)沒有返回值寫法
let say3:() ->Void = {
print("hi lnj")
}
say3()
/*
閉包表達(dá)式作為回調(diào)函數(shù)
*/
func showArray(array:[Int])
{
for number in array
{
print("\(number), ")
}
}
/*
// 缺點(diǎn), 不一定是小到大, 不一定是全部比較, 有可能只比較個(gè)位數(shù)
// 所以, 如何比較可以交給調(diào)用者決定
func bubbleSort(inout array:[Int])
{
let count = array.count;
for var i = 1; i < count; i++
{
for var j = 0; j < (count - i); j++
{
if array[j] > array[j + 1]
{
let temp = array[j]
array[j] = array[j + 1]
array[j + 1] = temp
}
}
}
}
*/
let cmp = {
(a: Int, b: Int) -> Int in
if a > b{
return 1;
}else if a < b
{
return -1;
}else
{
return 0;
}
}
func bubbleSort(inout array:[Int], cmp: (Int, Int) -> Int)
{
let count = array.count;
for var i = 1; i < count; i++
{
for var j = 0; j < (count - i); j++
{
if cmp(array[j], array[j + 1]) == -1
{
let temp = array[j]
array[j] = array[j + 1]
array[j + 1] = temp
}
}
}
}
var arr:Array<Int> = [31, 13, 52, 84, 5]
bubbleSort(&arr, cmp: cmp)
showArray(arr)
// 閉包作為參數(shù)傳遞
bubbleSort(&arr, cmp: {
(a: Int, b: Int) -> Int in
if a > b{
return 1;
}else if a < b
{
return -1;
}else
{
return 0;
}
})
print("---------------")
showArray(arr)
// 如果閉包是最后一個(gè)參數(shù), 可以直接將閉包寫到參數(shù)列表后面, 這樣可以提高閱讀性. 稱之為尾隨閉包
bubbleSort(&arr) {
(a: Int, b: Int) -> Int in
if a > b{
return 1;
}else if a < b
{
return -1;
}else
{
return 0;
}
}
// 閉包表達(dá)式優(yōu)化,
// 1.類型優(yōu)化, 由于函數(shù)中已經(jīng)聲明了閉包參數(shù)的類型, 所以傳入的實(shí)參可以不用寫類型
// 2.返回值優(yōu)化, 同理由于函數(shù)中已經(jīng)聲明了閉包的返回值類型,所以傳入的實(shí)參可以不用寫類型
// 3.參數(shù)優(yōu)化, swift可以使用$索引的方式來(lái)訪問(wèn)閉包的參數(shù), 默認(rèn)從0開始
bubbleSort(&arr){
// (a , b) -> Int in
// (a , b) in
if $0 > $1{
return 1;
}else if $0 < $1
{
return -1;
}else
{
return 0;
}
}
// 如果只有一條語(yǔ)句可以省略return
let hehe = {
"我是lnj"
}
print(hehe())
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。