While testing and debugging, I made a few tweaks to this category. I still haven't added reordering, but I think the methods that are here are now pretty solid. If you downloaded the earlier one, you probably want to grab the updated version, as it now codes defensively so problems aren't caused when a collection becomes empty by deletion (media item collections can't be created without at least one media item).
MPMediaItemCollection-Utils.h#import <Foundation/Foundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface MPMediaItemCollection(Utils)
- (MPMediaItem *)firstMediaItem;
- (MPMediaItem *)lastMediaItem;
- (MPMediaItem *)mediaItemAtIndex:(NSUInteger)index;
- (MPMediaItem *)mediaItemAfterItem:(MPMediaItem *)compare;
- (NSString *)titleForMediaItemAtIndex:(NSUInteger)index;
- (BOOL)containsItem:(MPMediaItem *)compare;
- (MPMediaItemCollection *)collectionByAppendingCollection:(MPMediaItemCollection *)otherCollection;
- (MPMediaItemCollection *)collectionByAppendingMediaItems:(NSArray *)items;
- (MPMediaItemCollection *)collectionByAppendingMediaItem:(MPMediaItem *)item;
- (MPMediaItemCollection *)collectionByDeletingMediaItems:(NSArray *)itemsToRemove;
- (MPMediaItemCollection *)collectionByDeletingMediaItem:(MPMediaItem *)itemToRemove;
- (MPMediaItemCollection *)collectionByDeletingMediaItemAtIndex:(NSUInteger)index;
- (MPMediaItemCollection *)collectionByDeletingMediaItemsFromIndex:(NSUInteger)from toIndex:(NSUInteger)to;
@end
MPMediaItemCollection-Utils.m#import "MPMediaItemCollection-Utils.h"
@implementation MPMediaItemCollection(Utils)
- (MPMediaItem *)firstMediaItem {
return [[self items] objectAtIndex:0];
}
- (MPMediaItem *)lastMediaItem {
return [[self items] lastObject];
}
- (MPMediaItem *)mediaItemAtIndex:(NSUInteger)index {
return [[self items] objectAtIndex:index];
}
- (MPMediaItem *)mediaItemAfterItem:(MPMediaItem *)compare {
NSArray *items = [self items];
for (MPMediaItem *oneItem in items) {
if ([oneItem isEqual:compare]) {
if (![[items lastObject] isEqual: oneItem])
return [items objectAtIndex:[items indexOfObject:oneItem] + 1];
}
}
return nil;
}
- (NSString *)titleForMediaItemAtIndex:(NSUInteger)index {
MPMediaItem *item = [[self items] objectAtIndex:index];
return [item valueForProperty:MPMediaItemPropertyTitle];
}
- (BOOL)containsItem:(MPMediaItem *)compare {
NSArray *items = [self items];
for (MPMediaItem *oneItem in items) {
if ([oneItem isEqual:compare])
return YES;
}
return NO;
}
- (MPMediaItemCollection *)collectionByAppendingCollection:(MPMediaItemCollection *)otherCollection {
return [self collectionByAppendingMediaItems:[otherCollection items]];
}
- (MPMediaItemCollection *)collectionByAppendingMediaItems:(NSArray *)items {
if (items == nil || [items count] == 0)
return nil;
NSMutableArray *appendCollection = [[[self items] mutableCopy] autorelease];
[appendCollection addObjectsFromArray:items];
return [MPMediaItemCollection collectionWithItems:appendCollection];
}
- (MPMediaItemCollection *)collectionByAppendingMediaItem:(MPMediaItem *)item {
if (item == nil)
return nil;
return [self collectionByAppendingMediaItems:[NSArray arrayWithObject:item]];
}
- (MPMediaItemCollection *)collectionByDeletingMediaItems:(NSArray *)itemsToRemove {
if (itemsToRemove == nil || [itemsToRemove count] == 0)
return [[self copy] autorelease];
NSMutableArray *items = [[[self items] mutableCopy] autorelease];
[items removeObjectsInArray:itemsToRemove];
return [MPMediaItemCollection collectionWithItems:items];
}
- (MPMediaItemCollection *)collectionByDeletingMediaItem:(MPMediaItem *)itemToRemove {
if (itemToRemove == nil)
return [[self copy] autorelease];
NSMutableArray *items = [[[self items] mutableCopy] autorelease];
[items removeObject:itemToRemove];
return [MPMediaItemCollection collectionWithItems:items];
}
- (MPMediaItemCollection *)collectionByDeletingMediaItemAtIndex:(NSUInteger)index {
NSMutableArray *items = [[[self items] mutableCopy] autorelease];
[items removeObjectAtIndex:index];
return [items count] > 0 ? [MPMediaItemCollection collectionWithItems:items] : nil;
}
- (MPMediaItemCollection *)collectionByDeletingMediaItemsFromIndex:(NSUInteger)from toIndex:(NSUInteger)to {
if (to < from) {
NSUInteger temp = from;
to = from;
from = temp;
}
NSMutableArray *items = [[[self items] mutableCopy] autorelease];
[items removeObjectsInRange:NSMakeRange(from, to - from)];
return [MPMediaItemCollection collectionWithItems:items];
}
@end