Can I avoid explicitly casting objects with a common subclass?
Posted
by prendio2
on Stack Overflow
See other posts from Stack Overflow
or by prendio2
Published on 2010-04-05T16:07:24Z
Indexed on
2010/04/05
16:13 UTC
Read the original article
Hit count: 279
I have an iPodLibraryGroup object and Artist and Album both inherit from it.
When it comes to my view controllers though I find that I'm duplicate lots of code, for example I have an ArtistListViewController and and AlbumListViewController even though they're both doing basically the same thing.
The reason I've ended up duplicating the code is because these view controllers each refer to either an Artist object or al Album object and I'm not sure how to set it up so that one view controller could handle both — these view controllers are mainly accessing methods that that the objects have in common from iPodLibraryGroup.
As an example, to hopefully make this clearer consider this code in AlbumListViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Album *album = nil;
album = [self albumForRowAtIndexPath:indexPath inTableView:tableView];
…
if (!album.thumbnail)
{
[self startThumbnailDownload:album forIndexPath:indexPath inTableView:tableView];
cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];
}
else
{
cell.imageView.image = album.thumbnail;
}
return cell;
}
This is essentially completely repeated (along with a hell of a lot more repeated code) in ArtistListViewController just so that I can typecast the local variable as an Artist instead of an Album.
Is there a way to not explicitly need to set Artist or Album here so that the same code could work for any object that is a child of iPodLibraryGroup?
© Stack Overflow or respective owner