Posts Tagged ‘Objective-C’

[iOS] Asynchronous Library in Objective-C

Tuesday, December 28th, 2010

I made a asynchronous library in Objective-C.
Here is Project file.

What is this ?

Asynchronous library is one of the CommandPettern.
Set a command, and execute it.
Then it can call method asynchronously.

Class Explanation

Command > Base class
WaitCommand > timer class
FunctionCommand > function class
SerialCommand > it call registered methods sequentially
ParallelCommand > it call registered methods at the same time

Inspired from
These ActionScript 3 libraries in Japan.
Commands (@fladdict)
Progression(@nium)

How to use

First, you copy below picture files.

WaitCommand test

- (void)commandCompleteHandler:(NSNotification *)aNotification
{
	NSLog(@"command complete !");
}

- (void)waitCommandTest
{
	WaitCommand *wc = [[WaitCommand alloc] initWithTime:1.0];
	[wc addEventListener:kCommandEventCommandComplete target:self listener:@selector(commandCompleteHandler:)];
	[wc execute];
	[wc release];
}

FunctionCommand test

- (void)commandCompleteHandler:(NSNotification *)aNotification
{
	NSLog(@"command complete !");
}

- (void)testFunction
{
	NSLog(@"test function");
}

- (void)functionCommandTest
{
	FunctionCommand *fc = [[FunctionCommand alloc] initWithTarget:self selector:@selector(testFunction)];
	[fc addEventListener:kCommandEventCommandComplete target:self listener:@selector(commandCompleteHandler:)];
	[fc execute];
	[fc release];
}

SerialCommand test

- (void)commandCompleteHandler:(NSNotification *)aNotification
{
	NSLog(@"command complete !");
}

- (void)testFunction
{
	NSLog(@"test function");
}

- (void)serialCommandTest
{
	SerialCommand *sc = [[SerialCommand alloc] init];
	WaitCommand *wc = [[WaitCommand alloc] initWithTime:1.5];
	FunctionCommand *fc = [[FunctionCommand alloc] initWithTarget:self selector:@selector(testFunction)];
	
	[sc addCommand:wc];
	[sc addCommand:fc];
	[sc addEventListener:kCommandEventCommandComplete target:self listener:@selector(commandCompleteHandler:)];
	[sc execute];

	[wc release];
	[fc release];
}

ParallelCommand test

- (void)commandCompleteHandler:(NSNotification *)aNotification
{
	NSLog(@"command complete !");
}

- (void)testFunction
{
	NSLog(@"test function");
}

- (void)parallelCommandTest
{
	ParallelCommand *pc = [[ParallelCommand alloc] init];
	WaitCommand *wc = [[WaitCommand alloc] initWithTime:1.5];
	FunctionCommand *fc = [[FunctionCommand alloc] initWithTarget:self selector:@selector(testFunction)];
	
	[pc addCommand:wc];
	[pc addCommand:fc];
	[pc addEventListener:kCommandEventCommandComplete target:self listener:@selector(commandCompleteHandler:)];
	[pc execute];

	[wc release];
	[fc release];
}

PrallelCommand and SerialCommand Mixed test

- (void)commandCompleteHandler:(NSNotification *)aNotification
{
	NSLog(@"command complete !");
}

- (void)testFunction
{
	NSLog(@"test function");
}

- (void)testFunction2:(id)arg1
{
	NSLog(@"testFunction 2, arg1 = %@", arg1);
}

- (void)testFunction3:(id)arg1 arg2:(id)arg2
{
	NSLog(@"testFunction 2, arg1 = %@, arg2 = %@", arg1, arg2);
}

- (void)parallelSerialMixedTest
{
	ParallelCommand *pc = [[ParallelCommand alloc] init];
	WaitCommand *wc = [[WaitCommand alloc] initWithTime:1.5];
	FunctionCommand *fc = [[FunctionCommand alloc] initWithTarget:self selector:@selector(testFunction)];
	FunctionCommand *fc2 = [[FunctionCommand alloc] initWithTarget:self selector:@selector(testFunction2:) withObject:[UIColor blueColor]];
	UIViewController *vc = [[UIViewController alloc] init];
	FunctionCommand *fc3 = [[FunctionCommand alloc] initWithTarget:self selector:@selector(testFunction3:arg2:) withObject:@"Hello command !" withObject:vc];
	[vc release];
	WaitCommand *wc2 = [[WaitCommand alloc] initWithTime:1.0];
	SerialCommand *sc = [[SerialCommand alloc] init];
	[pc addCommand:fc];
	[pc addCommand:fc2];
	[pc addCommand:fc3];
	
	[sc addCommand:wc];
	[sc addCommand:fc];
	[sc addCommand:wc2];
	[sc addCommand:pc];
	
	[sc addEventListener:kCommandEventCommandComplete target:self listener:@selector(commandCompleteHandler:)];
	[sc execute];
	
	[pc release];
	[wc release];
	[fc release];
	[fc2 release];
	[fc3 release];
	[wc2 release];
}

[iOS] EventDispatcher Class in Objective-C

Tuesday, December 28th, 2010

I am a iOS developer, and also a Flash Developer.
I wanted to EventDispatcher class in objective-c.
And I implemented it.

*update Feb.8.2011

IEventDispatcher.h

#import <Foundation/Foundation.h>

@protocol IEventDispatcher
- (void)addEventListener:(NSString *)type target:(id)target listener:(SEL)listener;
- (void)removeEventListener:(NSString *)type target:(id)target;
- (void)dispatchEvent:(NSString *)type;
@end

EventDispatcher.h

#import <Foundation/Foundation.h>
#import "IEventDispatcher.h"

@interface EventDispatcher : NSObject <IEventDispatcher> {
	NSMutableDictionary *targets;
}
@property (nonatomic, retain) NSMutableDictionary *targets;
- (void)addEventListener:(NSString *)type target:(id)target listener:(SEL)listener;
- (void)removeEventListener:(NSString *)type target:(id)target;
- (void)dispatchEvent:(NSString *)type;
@end

EventDispatcher.m


#import "EventDispatcher.h"


@implementation EventDispatcher
@synthesize targets;

- (id)init
{
	if ((self = [super init])) {
		NSMutableDictionary *mytargets = [[NSMutableDictionary alloc] init];
		self.targets = mytargets;
		[mytargets release];
	}
	return self;
}

- (void)addEventListener:(NSString *)type target:(id)target listener:(SEL)listener
{
	NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
	NSString *identifier = [type stringByAppendingFormat:@"_%d", &(*target)];
	if ([targets valueForKey:identifier] == nil) {
		[center addObserver:target selector:listener name:type object:self];
		[targets setObject:target forKey:identifier];
	}
}

- (void)removeEventListener:(NSString *)type target:(id)target
{
	NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
	NSString *identifier = [type stringByAppendingFormat:@"_%d", &(*target)];
	if ([targets valueForKey:identifier] != nil) {
		[center removeObserver:target name:type object:self];
		[targets removeObjectForKey:identifier];
	}
}

/**
 * Override this method. if you want to custom userInfo
 */
- (void)dispatchEvent:(NSString *)type
{
	NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
	[center postNotificationName:type object:self userInfo:nil];
}

- (void)dealloc
{
	NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
	for(NSString *identifier in targets){
		NSArray *idenntifierArr = [identifier componentsSeparatedByString:@"_"];
		[center removeObserver:[targets objectForKey:identifier] 
						  name:[idenntifierArr objectAtIndex:0] 
						object:self];
	}
	[targets removeAllObjects];
	[targets release];
	[super dealloc];
}
@end

how to use

set handler method

- (void)testHandler:(NSNotification *)aNotification
{
	NSLog(@"test handler");
	
	NSLog(@"name is %@", aNotification.name);
	NSLog(@"object is %@", aNotification.object);
	NSLog(@"info is %@", aNotification.userInfo);
}

and test

    EventDispatcher *myEd = [[[EventDispatcher alloc] init] autorelease];
	[myEd addEventListener:@"EventComplete" target:self listener:@selector(testHandler:)];
	[myEd dispatchEvent:@"EventComplete"];

Sample project file is here.

This article is very useful. Thanks
>> ustom Events in Objective-C | Raphael Wichmann