objective c - Is this the right way for a block inside a struct to access a member variable in the same struct? -


I am experimenting with Obj-C blocks and am trying to do a straight with two blocks in it Changing a block is what the second block does.

This is an ideal way to do something simple ... and there can be better ways to do this, but exercise means to understand the blocks. I here is the code, it does not work , So do I not remember / understand and / or am doing wrong?

  // Calculate my math operation options so that I can understand a bit more ... 0, 1, 2, etc. Also makes adding operation is easy because opTypeTotal // Before 1, the index of its operation will be Type-F Amem {opTypeAdd = 0, opTypeSubtract = 1, opTypeTotal} opType; // Not sure if (struct someMathStruct) * is correct, maybe wrong / / some MathStart has to pass an indicator, but the compiler // will not know about its existence for some lines after it ... Typedef (zero) (^ changeblocks) (optip, (straight some mathstrext) *); Type-F (zero) (^ math block) (int, int, int *); // Grab two blocks, then some mathstreck on the runtime typedef straight {mathBlock doMath; Changeblocks change operation; } Someth; // I want to declare an array of blocks of type mathBlock // / intent above the array index is to be consistent with the OT type above / I'm sure that this wrong mathBlock * m [opTypeTotal] = malloc (Sizeof (MathBlock *) * Opptiplate); // Only two common mathematical processes in the form of blocks [OPTIPE] = ^ (zero) (int a, int b, int * result) {* result = a + b;}; I [opptactrect] = ^ (zero) (int a, int b, int * result) {* result = a-b;}; // This block is going to change the second block in the straight / there is an opty type, and someMath is an indicator for // // Is it the right way to access the member variable of the straight? ChangeBlock should be easy to add operation to changeMe = ^ (zero) (opType a, SomeMath * b) as it is easy to switch matters (a) {case opTypeAdd: * b.doMath = m [a ]; break; Case Optimus: Default: * B.domath = M [A]; // catcher goes to subtracting everything}} ... SomeMath mathFun; Int theTotal = 0; // To work with a test int // do I need to copy the changing block? // or whatever I am doing I can do, as the block itself is not a unique mathematician. Exchange Operation = Changes; Mathfan- & gt; Change operation (opt-ad, and mathfan); MathFun-> DoMath (theTotal, 11, & amp; theTotal); // result should be 11 mathFun-> Change operation (optipubtract, and mathfan); MathFun-> DoMath (theTotal, 3, & amp; theTotal); // The result should be 8 NSLOG (@ "result:% D", total); // output "results: 8"  

The code starts working as you Once you fix the compilation errors you expect (the result is 8):

Compile with: GCC-O test test. M-Framemaker Foundation

  #import & lt; Foundation / Foundation. H & gt; // Calculate your math operation options so that I can understand a bit more from 0, 1, 2, etc ... also makes adding operations easier, because opTypeTotal // will be 1 of the index before its operation. Type-F Amem {opTypeAdd = 0, opTypeSubtract = 1, opTypeTotal} opType; Struct someMathStruct; // Next declare it as a form so that we can use it // changeBlock typedef typedef zero (^ changeBlock) (opType, struct someMathStruct *); Typefiff zero (^ math block) (integer, integer, integer *); // Grab two blocks, then some mathstreck on the runtime typedef straight {mathBlock doMath; Changeblocks change operation; } Someth; Int main () {// I want to declare array of type mathBlock // block / intent to correspond to the array index with oretiops, // mathBlock * m = calloc (opTypeTotal, sizeof (mathBlock *) described above ); Just two common mathematical processes in the form of blocks [OPTAP] = ^ (int a, int b, int * result) {* result = a + b;}; I [opeptesttrack] = ^ (int a, int b, int * result) {* result = a-b;}; ChangeBlock should replace changeMe = ^ (opType a, SomeMath * b), such that it is easy to switch matters (a) {case opTypeAdd: b-> DoMath = m [a]; break; Case Optimus: Default: B-> Dotmath = M [A]; // catcher goes to subtracting all}}; Some mathfans; Int theTotal = 0; // mathFun.changeOperation = a test int to work with changeMe; MathFun.changeOperation (Optics Aid, and Mathfen); MathFun.doMath (theTotal, 11, & amp; theTotal); // result must be 11 mathFun.changeOperation (opTypeSubtract, & amp; mathFun); MathFun.doMath (theTotal, 3, & amp; theTotal); // The result should be 8 NSLOG (@ "result:% D", total); // output should be "Result: 8"}  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -