Source

DTFoundation / DTFoundation / NSArray+DTExtras.m

//
//  NSArray+DTExtras.m
//  DTKit
//
//  Created by Daniel Tull on 30.09.2009.
//  Copyright 2009 Daniel Tull. All rights reserved.
//

#import "NSArray+DTExtras.h"


@implementation NSArray (DTExtras)

- (BOOL)isEmpty {
	return ([self count] == 0);
}

- (id)firstObject {
	if ([self isEmpty])
		return nil;
	
	return [self objectAtIndex:0];
}

- (NSArray *)subarrayWithObjectsOfClass:(Class)aClass {
	
	NSMutableArray *mArray = [[NSMutableArray alloc] init];
	
	for (id o in self)
		if ([o isKindOfClass:aClass])
			[mArray addObject:o];
	
	NSArray *subarray = [NSArray arrayWithArray:mArray];
	[mArray release];
	return subarray;
}

- (BOOL)containsObjectOfClass:(Class)aClass {
	
	for (id o in self)
		if ([o isKindOfClass:aClass])
			return YES;
	
	return NO;
}

- (NSArray *)dtSplitArrayIntoArraysWithCount:(NSInteger)aCount {
	
	NSInteger c = [self count];
	
	NSMutableArray *splitArrays = [[NSMutableArray alloc] init];
	
	NSRange range = NSMakeRange(0, aCount);
	
	while (c > 0) {
		
		if (c < aCount) range.length = c;
		
		NSArray *splitArray = [self subarrayWithRange:range];
		[splitArrays addObject:splitArray];
		
		range.location += range.length;
		
		c = c - aCount;
	}
	
	NSArray *returnArray = [splitArrays copy];
	[splitArrays release];
	return [returnArray autorelease];
}

@end