IOS Dev Intro - Blocks Programming Series 03
2016-06-12 10:21
447 查看
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Blocks/Articles/bxDeclaringCreating.html#//apple_ref/doc/uid/TP40007502-CH4-SW1
Blockvariablesholdreferencestoblocks.Youdeclarethemusingsyntaxsimilartothatyouusetodeclareapointertoafunction,exceptthatyouuse
of
Blocksalsosupportvariadic(
theargumentlist.
Blocksaredesignedtobefullytypesafebygivingthecompilerafullsetofmetadatatousetovalidateuseofblocks,parameterspassedtoblocks,andassignmentofthereturnvalue.Youcancastablock
referencetoapointerofarbitrarytypeandviceversa.Youcannot,however,dereferenceablockreferenceviathepointerdereferenceoperator(
ablock'ssizecannotbecomputedatcompiletime.
Youcanalsocreatetypesforblocks—doingsoisgenerallyconsideredtobebestpracticewhenyouuseablockwithagivensignatureinmultipleplaces:
Youusethe
argumentlistcontainedwithin
Thefollowingexampledefinesasimpleblockandassignsittoapreviouslydeclaredvariable(
endsaCstatement.
Ifyoudon’texplicitlydeclarethereturnvalueofablockexpression,itcanbeautomaticallyinferredfromthecontentsoftheblock.Ifthereturntypeisinferredandtheparameterlistis
thenyoucanomitthe
Atafilelevel,youcanuseablockasagloballiteral:
DeclaringandCreatingBlocks
DeclaringaBlockReference
Blockvariablesholdreferencestoblocks.Youdeclarethemusingsyntaxsimilartothatyouusetodeclareapointertoafunction,exceptthatyouuse ^instead
of
*.TheblocktypefullyinteroperateswiththerestoftheCtypesystem.Thefollowingareallvalidblockvariabledeclarations:
void(^blockReturningVoidWithVoidArgument)(void); |
int(^blockReturningIntWithIntAndCharArguments)(int,char); |
void(^arrayOfTenBlocksReturningVoidWithIntArgument[10])(int); |
...)arguments.Ablockthattakesnoargumentsmustspecify
voidin
theargumentlist.
Blocksaredesignedtobefullytypesafebygivingthecompilerafullsetofmetadatatousetovalidateuseofblocks,parameterspassedtoblocks,andassignmentofthereturnvalue.Youcancastablock
referencetoapointerofarbitrarytypeandviceversa.Youcannot,however,dereferenceablockreferenceviathepointerdereferenceoperator(
*)—thus
ablock'ssizecannotbecomputedatcompiletime.
Youcanalsocreatetypesforblocks—doingsoisgenerallyconsideredtobebestpracticewhenyouuseablockwithagivensignatureinmultipleplaces:
typedeffloat(^MyBlockType)(float,float); |
MyBlockTypemyFirstBlock=//...; |
MyBlockTypemySecondBlock=//...; |
CreatingaBlock
Youusethe ^operatortoindicatethebeginningofablockliteralexpression.Itmaybefollowedbyan
argumentlistcontainedwithin
().Thebodyoftheblockiscontainedwithin
{}.
Thefollowingexampledefinesasimpleblockandassignsittoapreviouslydeclaredvariable(
oneFrom)—heretheblockisfollowedbythenormal
;that
endsaCstatement.
float(^oneFrom)(float); |
oneFrom=^(floataFloat){ |
floatresult=aFloat-1.0; |
returnresult; |
}; |
void,
thenyoucanomitthe
(void)parameterlistaswell.Iforwhenmultiplereturnstatementsarepresent,theymustexactlymatch(usingcastingifnecessary).
GlobalBlocks
Atafilelevel,youcanuseablockasagloballiteral:#import<stdio.h> |
intGlobalInt=0; |
int(^getGlobalInt)(void)=^{returnGlobalInt;}; |
相关文章推荐
- IOS Dev Intro - Blocks Programming Series 02
- IOS Dev Intro - Blocks Programming Series 01
- iOS调用perform selector超过2个参数以上的方法
- iOS开发:编辑框格式化输入、删除、插入
- IOS之NSArray 中调用的方法详解
- iOS - 富文本AttributedString
- iOS平台下cookie的使用
- iOS ipv6 兼容
- iOS 笔记
- iOS小莲花Versions的SVN工具不能提交.a文件的解决方法
- iOS开发 引入支付宝需要注意的地方
- iOS9新特性(二)
- IPv6介绍及程序支持IPv6的障碍
- 关于 iOS socket 都在这里了
- iOS教你轻松打造瀑布流Layout
- iOS 独立开发记录(下)
- iOS 独立开发记录(上)
- 【代码笔记】iOS-通过颜色来生成一个纯色图片
- 【代码笔记】iOS-通过颜色来生成一个纯色图片
- AVCaptureDevice 曝光模式