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! While you're at it, check out some resources Treehouse students have shared here.

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

iOS

What 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
Thomas Nilsen
14,957 Points

Have 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];

Still don't really get it. What does...

[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]

...return anyway?

Thomas Nilsen
Thomas Nilsen
14,957 Points

you collectionView object calls the method:

- (id)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath*)indexPath

Which, like I said previously, returns 'id', which is a generic object.

how come it works even without

(UICollectionViewCell *)

in the expression? what for?