Customizing objc

Customizing Objective-C
NS_Customizing

1 NS_Customizing.h[ | ]

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface NSArray (Customizing)
- (NSArray*) shuffleArray;
- (NSArray*) removeLastObjectArray;
@end

@interface NSMutableArray (Customizing)
- (void) shuffle;
@end

@interface NSDictionary (Customizing)
- (NSMutableDictionary *)mutableDeepCopy;
@end

@interface NSDate (Customizing)
+ (NSDate*) distantFuture2;
@end

@interface NSTimer (Customizing)
- (void) pause;
- (void) resume;
- (BOOL) isPaused;
@end

2 NS_Customizing.m[ | ]

#import "NS_Customizing.h"

@implementation NSArray (Customizing)
- (NSArray*) shuffleArray {
	NSMutableArray* shuffledArray = [NSMutableArray arrayWithArray: self];
	[shuffledArray shuffle];
	return shuffledArray;
}
- (NSArray*) removeLastObjectArray {
	NSMutableArray* removedArray = [NSMutableArray arrayWithArray:self];
	[removedArray removeLastObject];
	return removedArray;
}
@end

@implementation NSMutableArray (Customizing)
- (void) shuffle {
	for (int i = [self count] - 1; i > 0; --i) {
		[self exchangeObjectAtIndex: random() % (i + 1) withObjectAtIndex: i]; 
	}
}
@end

@implementation NSDictionary (Customizing)
-(NSMutableDictionary*) mutableDeepCopy{
	NSMutableDictionary *ret=[[NSMutableDictionary alloc] initWithCapacity:[self count]];
	NSArray *keys=[self allKeys];
	for(id key in keys) {
		id oneValue=[self valueForKey:key];
		id oneCopy=nil;
		if([oneValue respondsToSelector:@selector(mutableDeepCopy)])oneCopy=[oneValue mutableDeepCopy];
		else if([oneValue respondsToSelector:@selector(mutableCopy)])oneCopy=[oneValue mutableCopy];
		if(oneCopy==nil)oneCopy=[oneValue copy];
		[ret setValue:oneCopy forKey:key];
	}
	return ret;
}
@end

@implementation NSDate (Customizing)
//4001-01-01 00:00:00 GMT -- distantFuture
+(NSDate*) distantFuture2 {
	NSString *str = @"2121-12-21";
	NSDateFormatter *df = [[NSDateFormatter alloc] init];
	[df setDateFormat:@"yyyy-MM-dd"];
	NSDate *date = [[NSDate alloc] init];
	[date autorelease];
	date = [df dateFromString:str];
	[df release];
	return date;
}
@end

@implementation NSTimer (Customizing)
-(void) pause {
	//NSLog(@"Timer [%@] is paused now.", [self userInfo]);
	[self setFireDate:[NSDate distantFuture2]];
}
- (void) resume {
	//NSLog(@"Timer [%@] is resumed now.", [self userInfo]);
	[self setFireDate:[NSDate dateWithTimeIntervalSinceNow:[self timeInterval]]];	
}
- (BOOL) isPaused {
	//NSLog(@"fireDate=[%@]", [self fireDate]);
	//NSLog(@"distantFuture=[%@]", [NSDate distantFuture2]);
	if( [[self fireDate] isEqualToDate:[NSDate distantFuture2]] ) return YES;
	return NO;
}

3 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}