Showing posts with label initWithTarget. Show all posts
Showing posts with label initWithTarget. Show all posts

Saturday, May 11, 2013

NSInvocationOperation initWithTarget example ios


initWithTarget :selector:object:

Returns an NSInvocationOperation object initialized with the specified target and selector.
- (id)initWithTarget:(id)target selector:(SEL)sel object:(id)arg
Parameters
target
The object defining the specified selector.
sel
The selector to invoke when running the operation. The selector may take 0 or 1 parameters; if it accepts a parameter, the type of that parameter must be id. The return type of the method may be void, a scalar value, or an object that can be returned as an id type.
arg
The parameter object to pass to the selector. If the selector does not take an argument, specify nil.
Return Value( NSInvocationOperation initWithTarget example )
An initialized NSInvocationOperation object or nil if the target object does not implement the specified selector.
Discussion( NSInvocationOperation initWithTarget example )
If you specify a selector with a non-void return type, you can get the return value by calling the resultmethod after the operation finishes executing. The receiver tells the invocation object to retain its arguments.
( NSInvocationOperation initWithTarget example )
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
               selector:@selector(loadImagesWithOperation:)
                 object:params];

( NSInvocationOperation initWithTarget example )
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
NSDictionary *argumentDictionary = [NSDictionary dictionaryWithObjectsAndKeys:object1, @"Object1Key", object2, @"Object2Key", nil];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(methodCall:) object:argumentDictionary];
[queue addOperation:operation];
[operation release];

( NSInvocationOperation initWithTarget example )
- (void)myMethod:(NSDictionary*)parameters 
{
    int a = [[parameters objectForKey:@"A"] intValue];
    int b = [[parameters objectForKey:@"B"] intValue];

    // do something with a and b
}


[[NSInvocationOperation alloc] 
    initWithTarget:self
          selector:@selector(myMethod:)
            object:[NSDictionary dictionaryWithObjectsAndKeys:
                     [NSNumber numberWithInt:123], @"A",
                     [NSNumber numberWithInt:456], @"B",
                     nil]];

Friday, April 19, 2013

NSThread initWithTarget example objc


initWithTarget:selector:object:

Returns an NSThread object initialized with the given arguments.
- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument
Parameters
target
The object to which the message specified by selector is sent.
selector
The selector for the message to send to target. This selector must take only one argument and must not have a return value.
argument
The single argument passed to the target. May be nil.
Return Value
An NSThread object initialized with the given arguments.
Discussion - [NSThread initWithTarget: selector: object:]
For non garbage-collected applications, the method selector is responsible for setting up an autorelease pool for the newly detached thread and freeing that pool before it exits. Garbage-collected applications do not need to create an autorelease pool.
The objects target and argument are retained during the execution of the detached thread. They are released when the thread finally exits.


Example


#import <Foundation/Foundation.h>
#define NOT_DONE 0
#define DONE 1

// this will need to be on some class that is calling the NSThread object
-(void) someFunc:(id) arg {
NSConditionLock* myLock = arg;
[myLock lock];
//do stuff that we need to know about in main
[myLock unlockWithCondition:DONE];
}


int main() {

NSConditionLock* finishedLock = [[NSConditionLock alloc]
initWithCondition: NOT_DONE];

NSThread* myThread = [[NSThread alloc] initWithTarget:self
selector:@selector(someFunc:) object:finishedLock];

[myThread start];

[finishedLock lockWhenCondition:DONE];

// now we can do whatever we need to do with the results from
// the thread we spawned

}