2014-02-28 59 views
-1
void (^configureCell)(PhotoCell*, Photo*) = ^(PhotoCell* cell, Photo* photo) { 
    cell.label.text = photo.name; 
}; 

我從來沒有見過塊中的「=」符號,這是如何工作的?混淆使用=在塊中

謝謝!

+3

你的頭銜非常缺乏描述性。 – dandan78

+0

如果我知道如何描述這個塊,我現在不會問這個問題,對嗎? – MCMatan

回答

2

它被分配到一個塊聲明configureCell變量。

這是塊變量定義(如即NSArray的*陣列):

void (^configureCell)(PhotoCell *, Photo *) 

這是塊定義:

^(PhotoCell* cell, Photo* photo) {...} 

可以使用該塊是這樣的:

PhotoCell *cell = [PhotoCell ...]; 
Photo *photo = [Photo ...]; 

// Execute block 
configureCell(cell, photo); 
4

如果你指的是該位

void (^configureCell)(PhotoCell*, Photo*) = ^(PhotoCell* cell, Photo* photo) {

這是分配塊的命名變量:configureCell

我建議你看看http://www.fuckingblocksyntax.com

0

看一看the Apple Documentation。 如果你編寫這樣的代碼塊,你可以調用像你在Java中那樣的方法。

,所以你可以通過像傳遞變量:

MyCustomCell *cell = configureCell(photoCellVariable, photoVariable); 

與其說這是這樣的消息,就像你通常做它Objective-C

MyCustomCell *cell = [ConfigurePhotoCell:photoCellVariable 
         andPhotoVariable:photoVariable];