Is it possible to explicitly exclude objects with a common subclass?

I have an iPodLibraryGroup object, and Artist and Album both inherit from it.

When it comes to the controllers of my view, although I find that I duplicate a lot of code, for example, I have ArtistListViewController and AlbumListViewController, although they both do basically the same thing.

The reason I ended up duplicating the code is because each of these view controllers refers to either an Artist object or an al Album object, and I'm not sure how to configure it so that one view controller can handle both - these controllers views generally access methods that shared objects relate to iPodLibraryGroup.

As an example, to reliably make this more understandable, 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 almost completely repeated (along with black much more repeating code) in the ArtistListViewController, so that I can give the local variable a variable as Artist, not an album.

Is there a way to not explicitly set Artist or Album here so that the same code can work for any object that is a child of iPodLibraryGroup?

+3
source share
2 answers

Based on the expression obtained on twitter , I created a protocol on iPodLibraryGroup as follows:

@protocol iPodLibraryGroup <NSObject>
@required

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain, readonly) NSString *sort_name;


- (void)downloadImageFromURL:(NSURL *)image_url;
- (void)cancelImageDownload;
- (void)downloadImageCannotStart;

@end


@interface iPodLibraryGroup : NSObject <iPodLibraryGroup> {


}

:

id <iPodLibraryGroup> source;

, , NSObject, :

?, , "source" (iPodLibraryGroup *) NSObject, :

[(iPodLibraryGroup *)source addObserver:self forKeyPath:@"pendingResponseForDetailedInfo" options:0 context:nil];
0

, ListViewController, iPodLibraryGroup, ArtistListViewController AlbumListViewController .

ListViewController, Artist/Album / , -.

+3

Source: https://habr.com/ru/post/1739864/


All Articles