2010-01-30 74 views
2

我寫了一個簡單的程序來了解objective-c的工作原理。這個程序是i-ching,一種基於六行反應的古代占卜,在六次發射三枚硬幣後計算出來,然後建立一個六角星形,這就是反應。非常基本的目標-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的每一行分配一個值。第一次發射應該在開始時填充價值,第二次在官員中填充......等等。 但是可以用開關盒輕鬆解決...雖然我不喜歡它。

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 
} 

但隨後,白衣這種解決方案我得到一個錯誤EXC_BAD_ACCESS 所以,很顯然我誤解陣列objective-是如何工作的c或c! 希望我已經說清楚了,是否有人能夠指出第一個問題的解決方案,以及我在第二個方案中做錯了什麼。

感謝 萊昂納多

回答

3

您已經創建了線的C數組 - 訪問你需要用C風格的數組訪問器的元素。

所以不是

[response objectAtIndex[0]] 

使用

response[0] 
+0

嗨,你好,是工作,但是我還有一個基本問題之一。 在接收方法中,我想做一個任務: Line therespone [6]; < - 類變量 - (id)buildSubView:(Line [])reponse {response} = response; < - 這會產生一個構建無效的賦值錯誤 ........ } 但我不能,因爲編譯器會抱怨「賦值中的不兼容類型」。 – Leonardo 2010-01-30 14:23:43