0

I'm learning objective-C and I was looking at some sample code from:

https://developer.apple.com/library/ios/samplecode/UsingPhotosFramework/Listings/SamplePhotosApp_AAPLAssetGridViewController_m.html#//apple_ref/doc/uid/TP40014575-SamplePhotosApp_AAPLAssetGridViewController_m-DontLinkElementID_8

I'm confused about this line of code here:

CGSize cellSize = ((UICollectionViewFlowLayout *)self.collectionViewLayout).itemSize;

I understand that it's trying to get the itemSize property and store it into cellSize, but I have no idea what ((UICollectionViewFlowLayout *)self.collectionViewLayout) is all about. Can someone break it down for me? Is there another way to write this line of code?

  • `self.collectionViewLayout` is of type `UICollectionViewLayout`. `UICollectionViewFlowLayout` (note the `Flow`) is a subclass of it. So `self.collectionViewLayout` is in this case of type `UICollectionViewFlowLayout`, but is generally of the higher type `UICollectionViewLayout`. Because the property `itemSize` is accessed, and this property only exists in a `UICollectionViewFlowLayout`, but **not** in a `UICollectionViewLayout`, `self.collectionViewLayout` has to be casted down, so it can be treated as `UICollectionViewFlowLayout` and `itemSize` can be accessed. – return true Oct 01 '14 at 21:00
  • This should have been an answer, but I could`t post one anymore. – return true Oct 01 '14 at 21:01

1 Answers1

0

What it means is:

Cast self.collectionViewLayout to be of UICollectionViewFlowLayout type. Then of self.collectionViewLayout get the itemSize property. Finally, save everything is a property of type CGSize.

I believe is a elegant and concise way of writing it.

carlodurso
  • 2,886
  • 4
  • 24
  • 37
  • To add on, the double sets of parenthesis allow you access object properties using the dot separator, in one line. – Carpetfizz Oct 01 '14 at 20:57