代码之家  ›  专栏  ›  技术社区  ›  Leonardo

非常基本的目标C问题

  •  2
  • Leonardo  · 技术社区  · 15 年前

    我编写了一个简单的程序来理解Objective-C是如何工作的。这个程序是易经,一个基于六行反应的古代占卜,在发射三枚硬币六次后计算,然后建立一个六边形作为回应。

    我坚持这一点,我确信有简单的解决办法。这就是我定义线条的方式,我知道这不是最好的设计,但我正在尽可能多地使用技术。 如果你发射一枚硬币,它可能是3或2,取决于侧面,3枚硬币可能产生6,7,8,9的值。

     /**
      * identifying a coin
      */
     typedef enum {
      head=3,
      tail=2
     } Coin;
    
     /**
      identify a line, three coins with a side value of
      2 and 3 can result in 6,7,8,9
      */
     typedef enum {
      yinMutable=tail+tail+tail, // 6 --> 7
      yang=tail+tail+head,  // 7 
      yin=head+head+tail,   // 8
      yangMutable=head+head+head // 9 --> 8
     } Line;
    
     /**
      The structure of hexagram from bottom "start" to top "end"
      */
     typedef struct {
      Line start;
      Line officer;
      Line transit;
      Line minister;
      Line lord;
      Line end;
     } Hexagram;
    

    我在这个设计中遇到的第一个问题是在六线形中的每一行指定一个值。第一次发射应该在Start中填入值,第二次发射的军官……等等。 但用开关盒很容易解决…尽管我不喜欢。

    1)第一个问题:我想知道是否有类似于javascript或类似C的函数 ForEach(Hexagram中的属性)允许我按声明顺序浏览属性,这将解决我的问题。

    2)第二个问题:作为一种替代方法,我使用了一系列行:

    Controller.m
    ....
    Line response[6]
    ....
    
    -(id) buildHexagram:... {
    
    for(i =0.....,i++).....
      response[i]=throwCoins;
    
    // I omit alloc view and the rest of the code...then
    [myview buildSubview:response]; 
    }
    
    
    ----------------------
    subView.m
    
    
    -(id) buildSubView:(Line[]) reponse {
    
    NSLog(@"response[0]=%o",[response objectAtIndex[0]]); <--- HERE I GOT THE ERROR
    }
    

    但是,在这个解决方案中,我得到了一个错误排除错误访问 所以很明显我误解了数组是如何在Objective-C或C中工作的! 希望我已经把自己说得足够清楚了,有人能指出第一个问题的解决方案吗,还有我在第二个选项中做了什么错误。

    谢谢 利奥纳多

    1 回复  |  直到 15 年前
        1
  •  3
  •   Robert Christie    15 年前

    您已经创建了一个C行数组-以访问需要使用C样式数组访问器的元素。

    因此,而不是

    [response objectAtIndex[0]]
    

    使用

    response[0]