views:

164

answers:

1

Hi all,

I wrote a simple program to understand how objective-c works. This program is the i-ching, an ancient divination based on six lines response, calculated after launching three coins for six times, and then build an hexagram which is the reponse.

I am stuck at this, that I am sure has simple solution. This is how I defined the lines, I know it's not the best design, but I am trying to use as much technology as possible. Supposing you launch a coin, it can be 3 or 2 depending on the side, three coins result in possible value 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;

The first problem I encounter with this design is to assign a value at each line in Hexagram. The first launch should fill value in start, the second in officer....and so on. But can be easily solved with a switch case...altough I don't like it.

1) First question: I wonder if there is some function like in javascript or c# like foreach (property in Hexagram) that let me browse the properties in their declaration order, that would solve my problem.

2) Second question: as an alternative way I used an array of Line:

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
}

but then, whit this solution I got an error EXC_BAD_ACCESS So obviously I am misunderstanding how array works in objective-c or c ! In the hope I have made myself clear enough, can someone point out the solution to the first question, and what I am doing wrong in the second option.

thanks Leonardo

+3  A: 

You've created a C array of Line - to access the elements you need to use C style array accessors.

So instead of

[response objectAtIndex[0]]

use

response[0]
Robert Christie
Hi...that worked, however I have another basic question one.In the receiving method I would like to do an assignment:Line therespone[6]; <-- class variable-(id) buildSubView:(Line[]) reponse {theresponse=response; <-- this raise a build invalid assignment error........}but I cannot, as the compiler would complain with an "incompatible types in assignment".
Leonardo