有关闭包的定义:
typealiascolsureName=(ParamClass,ParamClass?)->Void;
定义方法使用闭包时:
func colsureTestMethod:(colsureName):colsure;
eg:
// 定义回调闭包
typealias onSuccess=( MKNetworkOperation , String )-> Void ;
typealias
onError=(
NSError
)->
Void
;
//调用
private
func
operation_request(#urlstring:
String
,parmaDic:[
String
:
String
],httpMethod:
onSuccess
,onErr:
onError
){
let op: MKNetworkOperation = MKNetworkOperation (URLString: urlstring,params: parmaDic,httpMethod: httpMethod);
op. addCompletionHandler ({ (mop: MKNetworkOperation !) -> Void in
onSucc(mop,mop. responseString ());
},errorHandler: { (mop: MKNetworkOperation !,err: NSError !) -> in
onErr(err);
});
net_engine ?. enqueueOperation (op);
let op: MKNetworkOperation = MKNetworkOperation (URLString: urlstring,params: parmaDic,httpMethod: httpMethod);
op. addCompletionHandler ({ (mop: MKNetworkOperation !) -> Void in
onSucc(mop,mop. responseString ());
},errorHandler: { (mop: MKNetworkOperation !,err: NSError !) -> in
onErr(err);
});
net_engine ?. enqueueOperation (op);
}
func
logtrue(predicate:(
Int
,170)">Int
)->
Bool
{
var a = 100
var b = 101
return predicate(a,b)>a*b
var a = 100
var b = 101
return predicate(a,b)>a*b
}
输出结果:false
var flag = logtrue { (a: Int ) -> Int in
return a+c
}
关于@autoclosure
func
logbool(
@autoclosure
predicate:()->
Bool
){
if predicate(){
print ( "true" )
} else {
"false" )
}
if predicate(){
print ( "true" )
} else {
"false" )
}
}
用
@autoclosure 定义的
闭包
,可以直接使用类似 “
logbool
(
2
>
1
)” 的写法来使用 或者“
func
logboolValue(predicate:()->
"false"
)
}
}
}
}
logboolValue
({
1
})
原文链接:https://www.f2er.com/swift/325858.html