在前面一些学习中,原本把闭包给理解成了block尽管有很多相似之处,但block还是有他自己的独特之外。近日,在写oc/swift混合编码时,有时候需要swift回调oc,oc回调swift 。
因此我把swift中的 block 常见的声明和写法作一个总结。以免后续忘了,好查阅。废话不说多,上例子:
//
// blockDemo.swift
// swiftDemo
//
// Created by apple on 14-6-29.
// Copyright (c) 2014年 fengsh. All rights reserved.
//
import Foundation
//无参无返回值
typealias funcBlock = () -> () //或者 () -> Void
//返回值是String
typealias funcBlockA = (Int,Int) -> String
//返回值是一个函数指针,入参为String
typealias funcBlockB = (Int,Int) -> (String)->()
//返回值是一个函数指针,入参为String 返回值也是String
typealias funcBlockC = (Int,Int) -> (String)->String
class blockDemo
{
//block作为属性变量
var blockProperty : (Int,Int) -> String = {a,b in return ""/**/} // 带初始化方式
var blockPropertyNoReturn : (String) -> () = {param in }
var blockPropertyA : funcBlockA? //这写法就可以初始时为nil了,因为生命周其中,(理想状态)可能为nil所以用?
var blockPropertyB : funcBlockB! //这写法也可以初始时为nil了,因为生命周其中,(理想状态)认为不可能为nil,所以用!
init()
{
println("blockPropertyA = \(blockPropertyA) , blockPropertyB = \(blockPropertyB)")
println("blockProperty = \(blockProperty) , blockPropertyNoReturn = \(blockPropertyNoReturn)")
}
func testProperty(tag:Int)
{
switch (tag)
{
case 1:
self.blockPropertyNoReturn("OK GOOD")
case 2:
if let exsistBlock = self.blockPropertyA
{
let result = self.blockPropertyA!(7,8)
println("result = \(result)")
}
case 3:
if let exsistBlock = self.blockPropertyB
{
let fc = self.blockPropertyB(1,2)
fc("输出")
}
default:
let ret = self.blockProperty(3,4)
println(ret)
}
}
//block作为函数参数
func testBlock(blockfunc:funcBlock!)//使用!号不需要再解包
{
if let exsistblock = blockfunc
{
blockfunc() //无参无返回
}
}
func testBlockA(blockfunc:funcBlockA!)
{
if let exsistblock = blockfunc
{
let retstr = blockfunc(5,6)
println(retstr)
}
}
func testBlockB(blockfunc:funcBlockB!)
{
if let exsistblock = blockfunc
{
let retfunc = blockfunc(5,6)
retfunc("结果是")
}
}
func testBlockC(blockfunc:funcBlockC!)
{
if let exsistblock = blockfunc
{
let retfunc = blockfunc(5,6)
let str = retfunc("最终果结是")
println(str)
}
}
}
执行测试码:
var bk = blockDemo()
//block设置前,啥也没有输出
bk.testProperty(0)
bk.testProperty(1)
bk.testProperty(2)
bk.testProperty(3)
println("==============设置block属性================")
bk.blockProperty = {
(a :Int,b:Int) -> String in
let c = a*100+b
return "\(a)*100+\(b) = \(c)"
}
bk.testProperty(0)
bk.blockPropertyNoReturn = {
(param:String) -> () in
println("input param value is : \(param)")
}
bk.testProperty(1)
bk.blockPropertyA = {
(a:Int,b:Int) -> String in
let c = a*100+b*200
return "\(a)*100+\(b)*200 = \(c)"
}
bk.testProperty(2)
bk.blockPropertyB = {
(a:Int,b:Int) -> (String)->() in
func sumprint(result:String)
{
let c = a + b;
println("sumprint func print:parame :\(result) \(a) + \(b) = \(c)")
}
return sumprint
}
bk.testProperty(3)
println("==============属性block完成================")
println("==============函数block为nil时无输出================")
bk.testBlock(nil)
bk.testBlockA(nil)
bk.testBlockB(nil)
bk.testBlockC(nil)
println("==============函数block操作================")
bk.testBlock({
//do something
println("无参无返回值block 执行")
})
bk.testBlockA({
(a:Int,b:Int) -> String in
let c = a*400+b*1000
return "\(a)*400 + \(b)*1000 is \(c)"
})
bk.testBlockB({
(a:Int,b:Int) -> (String)->() in
func sumprint(result:String)
{
let c = a / b;
println("sumprint func print:parame :\(result) \(a) / \(b) = \(c)")
}
return sumprint
})
bk.testBlockC({
(a:Int,b:Int) -> (String)->String in
func sumrsult(res:String) -> String
{
let c = a*a+b*a
return "\(res) \(a)*\(a)+\(b)*\(a) = \(c)"
}
return sumrsult
})
以上包括了block声明和使用
下面是输出结果
blockPropertyA = nil , blockPropertyB = nil
blockProperty = (Function) , blockPropertyNoReturn = (Function)
==============设置block属性================
3*100+4 = 304
input param value is : OK GOOD
result = 7*100+8*200 = 2300
sumprint func print:parame :输出 1 + 2 = 3
==============属性block完成================
==============函数block为nil时无输出================
==============函数block操作================
无参无返回值block 执行
5*400 + 6*1000 is 8000
sumprint func print:parame :结果是 5 / 6 = 0
最终果结是 5*5+6*5 = 55
网友评论