Study Notes 10 (Block) of swift Language)

Source: Internet
Author: User

Fengsh998 original address: Sorry!


In the previous studies, the closure was originally understood as a block. Although there are many similarities, the block still has its own uniqueness. Recently, when writing OC/SWIFT mixed encoding, sometimes swift calls back OC and OC calls back Swift.

Therefore, I will summarize the common statements and statements of block in swift. So that you do not forget it later. There is not much nonsense. The example above:

//// Blockdemo. swift // swiftdemo /// created by Apple on 14-6-29. // copyright (c) February 11, 2014 fensh. all rights reserved. // import Foundation // No parameter no return value typealias funcblock = ()-> () // or ()-> void // the return value is stringtypealias funcblocka = (INT, INT) -> string // the return value is a function pointer. The input parameter is stringtypealias funcblockb = (INT, INT)-> (string)-> () // the return value is a function pointer, if the input parameter is string, the returned value is also stringtypealias funcblockc = (INT, INT)-> (string)-> stringcl Ass blockdemo {// block as the property variable VAR blockproperty: (INT, INT)-> string = {, B In return ""/** // With initialization method var blockpropertynoreturn: (string)-> () = {Param in} var blockpropertya: funcblocka? // This method can be used for nil at the beginning, because the (ideal) state in the lifecycle may be nil? VaR blockpropertyb: funcblockb! // This statement can also be nil at the beginning, because the (ideal) state in the lifecycle is considered impossible to be nil, so it is used! 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 ("output")} default: Let ret = self. blockproperty (3, 4) println (RET) }}// block as the function parameter func testblock (blockfunc: funcblock !) // Use! {If let exsistblock = blockfunc {blockfunc () // No parameter is returned} 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 ("result is")} func testblockc (blockfunc: funcblockc !) {If let exsistblock = blockfunc {Let retfunc = blockfunc (5, 6) Let STR = retfunc ("the final result is") println (STR )}}}

Run the test code:

VaR BK = blockdemo () // no BK is output before block is set. testproperty (0) BK. testproperty (1) BK. testproperty (2) BK. testproperty (3) println ("================== set block attributes ===========================") 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 ("================= attribute block completion ============================= ") println ("================= when the function block is nil, no output ====================== ") BK. testblock (NiL) BK. testblocka (NiL) BK. testblockb (NiL) BK. testblockc (nil) println ("================= function block operations ============================") BK. testblock ({// do something println ("No parameter, no return value, block execution")}) 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) * \ () = \ (c) "} return sumrsult })
The block statements and usage
The output result is as follows:

Blockpropertya = nil, blockpropertyb = nilblockproperty = (function), blockpropertynoreturn = (function) ================== set block attributes ============================= 3*100 + 4 = 304 input Param value is: OK goodresult = 7*100 + 8*200 = 2300 sumprint func print: parame: output 1 + 2 = 3 ===================== attribute block completion ============================== =============== no output when the function block is nil ======================= ============ function block operations ====================== No parameter No Return Value block execution 5*400 + 6*1000 is 8000 sumprint func print: parame: The result is 5/6 = 0. The final result is 5*5 + 6*5 = 55.

Well, I think this is clearer than the description of the text. If you have any questions or other strange statements, you are welcome to kick off the gym .....

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.