您的位置:首页 > 移动开发 > Swift

swift基本语法(总结提炼版)之012 swift 之闭包

2016-04-05 23:23 399 查看

一:swift 之闭包就相当于objective-c中的block,先来段block代码

#import "ViewController.h"

typedef void (^FINISHED)();

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self loadData];

[self loadData2:^{
NSLog(@"执行回调");
}];

[self loadData3:@"tinghou" finished:^{
NSLog(@"执行回调");
}];
}

- (void)loadData2:(void (^)())finished {
NSLog(@"耗时操作");
finished();
}

- (void)loadData3:(NSString *)name finished:(void (^)())finished
{
NSLog(@"name = %@", name);
finished();
}

- (void)loadData{
dispatch_async(dispatch_get_global_queue(0, 0), ^{

NSLog(@"耗时操作 %@", [NSThread currentThread]);

dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"回到主线程更新UI %@", [NSThread currentThread]);
});
});
}
上面代码运行结果:

2016-04-05 23:04:21.713 block[4430:97813] 耗时操作 <NSThread: 0x7fc38ad14830>{number = 2, name = (null)}
2016-04-05 23:04:21.713 block[4430:97709] 耗时操作
2016-04-05 23:04:21.714 block[4430:97709] 执行回调
2016-04-05 23:04:21.714 block[4430:97709] name = tinghou
2016-04-05 23:04:21.714 block[4430:97709] 执行回调
2016-04-05 23:04:21.720 block[4430:97709] 回到主线程更新UI <NSThread: 0x7fc38ad034e0>{number = 1, name = main}


二.闭包

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
/*
闭包 类似于 block
block是C语言的, 类似于匿名函数
闭包是定义函数的, Swift中函数就是一种特殊的闭包
闭包的使用和block一样, 用于保存一段代码, 用作回调, 用作执行耗时操作
闭包格式
{
(参数) -> 返回值类型
in
执行语句
}
*/
//        loadData()
/*
loadData2 { () -> () in
print("执行回调")
}
*/
/*
闭包简写:
1.如果没有参数, 没有返回值, in和in之前的东西可以省略
2.如果闭包是函数的最后一个参数, 可以写在()后面  -- 尾随闭包
3.如果只有一个闭包参数, 那么()也可以省略  -- 尾随闭包
*/
/*
loadData2 ({
print("执行回调")
})
*/
/*
loadData2(){
print("执行回调")
}
*/
loadData2{
print("执行回调")
}

loadData3("tinghou") { () -> () in
print("执行回调")
}
}

func loadData3(name: String, finished: () -> ()){
print("name = \(name)")
finished()
}

func loadData2(finished: () -> ()){
print("耗时操作")

// 回调
finished()
}

func loadData(){
dispatch_async(dispatch_get_global_queue(0, 0)) { () -> Void in
print(NSThread.currentThread())
print("耗时操作")

dispatch_async(dispatch_get_main_queue(), { () -> Void in
print(NSThread.currentThread())
print("回到主线程更新UI")
})
}
}

}


上面代码打印:
耗时操作
执行回调
name = tinghou
执行回调


二.一 闭包的返回值

let sc = UIScrollView(frame: CGRect(x: 0, y: 100, width: 375, height: 44))
sc.backgroundColor = UIColor.redColor()
let count = 15
let width = 50
for i in 0..<count{
let label = UILabel()
label.backgroundColor = UIColor.greenColor()
label.textColor = UIColor.darkGrayColor()
label.font = UIFont.systemFontOfSize(17)
label.text = "text\(i)"
label.frame = CGRect(x: i * width, y: 0, width: width, height: 44)
sc.addSubview(label)
}

sc.contentSize = CGSize(width: count * width, height: 44)
view.addSubview(sc)
效果:



使用闭包实现上述相同功能
creatScrollView { () -> Int in
return 5;
}

func creatScrollView(btnCount:() -> Int)
{
let sc = UIScrollView(frame: CGRect(x: 0, y: 100, width: 375, height: 44))
sc.backgroundColor = UIColor.redColor()

//            let count = 15
let count = btnCount
let width = 50
for i in 0..<count{
let label = UILabel()
label.backgroundColor = UIColor.greenColor()
label.textColor = UIColor.darkGrayColor()
label.font = UIFont.systemFontOfSize(17)
label.text = "text\(i)"
label.frame = CGRect(x: i * width, y: 0, width: width, height: 44)
sc.addSubview(label)
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: