这是结构内的块访问同一结构中的成员变量的正确方法吗? [英] Is this the right way for a block inside a struct to access a member variable in the same struct?

查看:69
本文介绍了这是结构内的块访问同一结构中的成员变量的正确方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Obj-C块,并尝试在其中包含两个块的结构中,一个块将更改另一个块的功能.

I'm experimenting with Obj-C blocks and trying to have a struct with two blocks in it where one block is to change what the other block does.

这是做一些简单事情的一种真正的回旋方式……可能会有更好的方法来做,但是练习的目的是让我理解障碍.这是代码,它不起作用,那么我缺少/不了解和/或做错了什么?

this is a really roundabout way to do something simple... and there may be better ways to do it, but the point of the exercise is for me to understand blocks. here's the code , it doesn't work, so what am I missing/not understanding and/or doing wrong?

//enumerate my math operation options so i can have something more understandable
//than 0, 1, 2, etc... also makes it easier to add operations, as opTypeTotal
//will be 1 plus the index of the operation before it.
typedef enum
{
 opTypeAdd = 0,
 opTypeSubtract = 1,
 opTypeTotal
} opType; 

//not sure if (struct someMathStruct)* is correct, probably is wrong
//the intent is to pass a pointer to someMathStruct, but the compiler
//won't know about its existance until a few lines later...
typedef (void)(^changeBlock)(opType,(struct someMathStruct)*);
typedef (void)(^mathBlock)(int,int,int*);

//hold two blocks, to be defined later at runtime
typedef struct someMathStruct{
 mathBlock doMath;
 changeBlock changeOperation;
} SomeMath;

//i want to declare an array of blocks of type mathBlock
//the intent is to have the array index to correspond with the opTypes enumerated above
//almost certain i'm doing this wrong
mathBlock *m[opTypeTotal] = malloc(sizeof(mathBlock *)*opTypeTotal);

//just two simple math operations as blocks
m[opTypeAdd] = ^(void)(int a,int b,int *result){*result = a+b;};
m[opTypeSubtract] = ^(void)(int a,int b,int *result){*result = a-b;};

//this block is what's supposed to change the other block in the struct
//it takes an opType, and a pointer to the SomeMath struct
//is this the right way to access the member variables of the struct?
changeBlock changeMe = ^(void)(opType a, SomeMath *b) {
  //should make adding operations as easy as just adding cases
 switch (a)
 {
  case opTypeAdd: *b.doMath=m[a]; break;
  case opTypeSubtract:
  default:  *b.doMath=m[a];  //catch-all goes to subtracting
 }
}

...

SomeMath mathFun;
int theTotal = 0;  //a test int to work with

//do i need to copy the changeMe block?
//or can i just do what i'm doing here as the block itself isn't unique
mathFun.changeOperation = changeMe;

mathFun->changeOperation(opTypeAdd, &mathFun);
mathFun->doMath(theTotal,11,&theTotal); //result should be 11

mathFun->changeOperation(opTypeSubtract, &mathFun);
mathFun->doMath(theTotal,3,&theTotal); //result should be 8

NSLog(@"the result: %d",theTotal); //should output "the result: 8"

推荐答案

修复了编译错误后,代码似乎按预期工作(结果为8):

The code seems to work as you expect (the result is 8) once you fix the compilation errors:

编译为:gcc -o test test.m -framework Foundation

#import <Foundation/Foundation.h>

//enumerate my math operation options so i can have something more understandable
//than 0, 1, 2, etc... also makes it easier to add operations, as opTypeTotal
//will be 1 plus the index of the operation before it.
typedef enum
{
 opTypeAdd = 0,
 opTypeSubtract = 1,
 opTypeTotal
} opType; 

struct someMathStruct; // Forward declare this as a type so we can use it in the 
                       // changeBlock typedef

typedef void (^changeBlock) (opType,struct someMathStruct*);
typedef void (^mathBlock) (int,int,int*);

//hold two blocks, to be defined later at runtime
typedef struct someMathStruct{
 mathBlock doMath;
 changeBlock changeOperation;
} SomeMath;


int main()
{

    //i want to declare an array of blocks of type mathBlock
    //the intent is to have the array index to correspond with the opTypes
    // enumerated above
    mathBlock *m = calloc(opTypeTotal, sizeof(mathBlock *));

    //just two simple math operations as blocks
    m[opTypeAdd] = ^(int a,int b,int *result){*result = a+b;};
    m[opTypeSubtract] = ^(int a,int b,int *result){*result = a-b;};

    changeBlock changeMe = ^(opType a, SomeMath *b) {
      //should make adding operations as easy as just adding cases
     switch (a)
     {
      case opTypeAdd: b->doMath = m[a]; break;
      case opTypeSubtract:
      default:  b->doMath = m[a];  //catch-all goes to subtracting
     }
    };

    SomeMath mathFun;
    int theTotal = 0;  //a test int to work with

    mathFun.changeOperation = changeMe;

    mathFun.changeOperation(opTypeAdd, &mathFun);
    mathFun.doMath(theTotal,11,&theTotal); //result should be 11

    mathFun.changeOperation(opTypeSubtract, &mathFun);
    mathFun.doMath(theTotal,3,&theTotal); //result should be 8

    NSLog(@"the result: %d",theTotal); //should output "the result: 8"
}

这篇关于这是结构内的块访问同一结构中的成员变量的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆