Skip to content

Instantly share code, notes, and snippets.

@luca-bernardi
Created February 18, 2013 10:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save luca-bernardi/4976541 to your computer and use it in GitHub Desktop.
Save luca-bernardi/4976541 to your computer and use it in GitHub Desktop.
How to use inline block to better organize a block of code
__weak typeof(self) weakSelf = self;
self.collectionView = (UICollectionView *)^{
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:weakSelf.view.bounds
collectionViewLayout:flowLayout];
collectionView.delegate = weakSelf;
collectionView.dataSource = weakSelf;
return collectionView;
}();
@kylef
Copy link

kylef commented Feb 18, 2014

This is totally overkill, you can do the following:

self.collectionView = ({
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView;
});

@luca-bernardi
Copy link
Author

@kylef I'm not doing this anymore and indeed I'm doing the same you were suggesting, even though is still see the problem that someone that is not familiar with this trick might take sometimes to understand the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment