我写了一个简单的程序来理解objective-c是如何工作的。这个程序就是易经,一种基于六行响应的古老占卜,在发射三枚硬币六次后计算,然后构建一个卦,即响应。
我坚持这一点,我确信有简单的解决方案。这就是我定义线条的方式,我知道这不是最好的设计,但我正在尝试尽可能多地使用技术。假设你发射一枚硬币,它可以是 3 或 2,具体取决于侧面,三个硬币的可能值是 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;
我在这个设计中遇到的第一个问题是在 Hexagram 中的每一行分配一个值。第一次启动应该在 start 中填写 value,第二次在 officer.... 等等。但是可以通过开关盒轻松解决……尽管我不喜欢它。
1)第一个问题:我想知道是否有一些像javascript或c#这样的函数,比如foreach(Hexagram中的属性),让我按照声明顺序浏览属性,这可以解决我的问题。
2)第二个问题:作为一种替代方式,我使用了一个 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
}
但是后来,这个解决方案出现了一个错误 EXC_BAD_ACCESS 所以很明显我误解了数组在objective-c或c中的工作原理!希望我已经足够清楚,有人可以指出第一个问题的解决方案,以及我在第二个选项中做错了什么。
谢谢莱昂纳多