Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Joseph Lau
5,420 PointsWhat does this mean?
I was following a tutorial for this. I see this kind of stuff once in a while but what is the point of adding (UICollectionViewCell *) within this line of code?? I tried removing it and the app ran alright.
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
4 Answers

Thomas Nilsen
14,957 PointsHave a look at this first: http://www.tutorialspoint.com/objective_c/objective_c_type_casting.htm
Now, if you have a look at the method
- (id)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath*)indexPath
it returns an 'id', which means 'any kind of object'. But since you know what kind of object you're gonna be using you can write this:
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

Joseph Lau
5,420 PointsStill don't really get it. What does...
[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]
...return anyway?

Thomas Nilsen
14,957 Pointsyou collectionView object calls the method:
- (id)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath*)indexPath
Which, like I said previously, returns 'id', which is a generic object.

Joseph Lau
5,420 Pointshow come it works even without
(UICollectionViewCell *)
in the expression? what for?