2013-11-20 36 views
0

我有一段代碼,似乎沒有工作,因爲預期的表達式錯誤。預期的表達式錯誤目標c

NSMutableArray *worldData =[[NSMutableArray alloc] initWithArray:@[ 

         @[ @1, @2, @3, @4,@5,@6], 
         @[ @1, @2, @3, @4,@5,@6], 
         @[ @1, @2, @3, @4,@5,@6], 
         @[ @1, @2, @3, @4,@5,@6], 

         ]]; 
int *x = 1; 

if (int 1 == (worldData objectAtIndex:1)){ 

    UIImage *block31 = [UIImage imageNamed: 
         @"grass2.png"]; 
} 

} 

回答

1

我不知道這是什麼真正的應該是:

int *x = 1; 

if (int 1 == (worldData objectAtIndex:1)){ 

我想你大概的意思是:

if ([[[worldData objectAtIndex:1] objectAtIndex:1] intValue] == 1) 

或更簡潔,可以使用標和文字:

if ([worldData[1][1] isEqual: @1]) 
+1

'(worldData [1] == @ 1)'這是不行的....:P –

+0

你說得對,我我以前不知道直接的比較數字文字有未定義的行爲。 –

1

你不需要在if塊中聲明int。剛剛改寫爲:

if (1 == [[[worldData objectAtIndex:1] objectAtIndex:1] intValue]){ 
+0

編輯之前,我可以證明我downvote! :P –

+0

愛只解決一個問題,注意到這不是唯一的問題。 –

0
int *x = 1; 

指針創建變量的存儲地址。

應該是這樣的:

int num = 1; 
int *x = # 

另一個問題:

if (int 1 == (worldData objectAtIndex:1)){ 

以上是沒有意義的。

正在比較NSNumber值與int。可以將一個類型轉換爲NSNumber或另一個轉換爲intValue。

if ([@1 isEqual: worldData[1]]) //[worldData objectAtIndex:1] 

if (1 == [worldData[1] intValue]) 

你被[worldData objectAtIndex:]完整陣列試圖訪問。所以你不能將它們與一個整數進行比較。要麼你需要另外一個數組,要麼你需要深入到數組的第二維。

[[worldData objectAtIndex:] objectAtIndex:] 

,或者

worldData[row][col] 
+0

您的==不會工作,他們正在比較'int'到'NSNumber' –