2013-05-05 44 views
0

我試圖獲取NSMutableArray的元素,將它們轉換爲名稱爲UIImageViews的字符串,並將所有圖像更改爲一個圖像。我使用這個for循環:XCode - 將數組元素轉換爲字符串

for (int i = 0; i < [self.array count]; i++) 
    NSString *currentelement = [self.array objectAtIndex:i] 
    [UIImageView * theImageView = [self valueForKey:currentelement] 
    [theImageView setImage:newimage]; 

,但它給了我第二行的錯誤:預計表達任何想法,爲什麼?

+0

你在你的self.array中有什麼?他們是你的圖像瀏覽器的字符串名稱? – 2013-05-05 11:03:39

+1

你福爾戈半冒號結束的C語句和你真的想環路剛剛超過第二行 - 我認爲你有forgottem的{} - 提示alwys使用大括號所有環路 – Mark 2013-05-05 11:04:40

+0

是的,這是一個可變數組具有字符串已經在它 – user2342394 2013-05-05 11:06:03

回答

2

您缺少一些基本的C標點符號。你忘記了用分號結束C語句,你真的想循環到第二行 - 我想你已經忘記了{} - 提示alwys在所有循環中都使用了大括號。

所以代碼會像

for (int i = 0; i < [self.array count]; i++) 
{ 
    NSString *currentelement = [self.array objectAtIndex:i]; 
    UIImageView * theImageView = [self valueForKey:currentelement]; 
    [theImageView setImage:newimage]; 
} 

也[用於消息,應該只出現在一個assignement的右手邊(=

我建議你需要看一些C和Objective C教程來顯示正確的代碼並描述語法。

+0

謝謝,這很完美! – user2342394 2013-05-05 11:19:19

0

你忘了分號和第三行是錯誤的:

你的代碼更改爲:

for (int i = 0; i < [self.array count]; i++) { 
    NSString *currentelement = [self.array objectAtIndex:i]; 
    UIImageView *theImageView = [self valueForKey:currentelement]; 
    [theImageView setImage:newimage]; 
} 

使用[]意味着我們將消息發送到一個對象。我們不能在郵件中使用=登錄。

+0

@ downvoter:你能解釋我的答案有什麼問題,以便我可以更新它嗎?謝謝! – HAS 2016-08-31 13:55:40