Skip to main content

how to create an "array of selectors' in objective-c


i'm using the iphone sdk (3.0) and i'm trying to create an array of selectors to invoke a variety of methods within one class.



Obviously, I'm doing something wrong (I think @selector isn't considered a class and so stuffing them into an NSArray isn't working).



I tried this, but it's obviously wrong.



Is there a simple way to have an array of selectors like this? Or is there a better way to iterate through a collection of methods?



Thanks for any insight!!




selectors = [NSArray arrayWithObjects:
@selector(method1),
@selector(method2),
@selector(method3),
@selector(method4),
@selector(method5),
@selector(method6),
@selector(method7), nil];

for (int i = 0; i < [selectors count]; i++) {
if ([self performSelector:[selectors objectAtIndex:i]]) // do stuff;
}


Source: Tips4allCCNA FINAL EXAM

Comments

  1. Could you store strings and use NSSelectorFromString?

    From the docs

    NSSelectorFromString


    Returns the selector with a given name.

    SEL NSSelectorFromString (
    NSString *aSelectorName
    );

    ReplyDelete
  2. This creates an object out of selector:

    [NSValue valueWithPointer:@selector(x)]

    ReplyDelete
  3. Why not just use a simple C array?

    static const SEL selectors[] = {@selector(method1),
    ....
    @selector(method7)};

    ...

    for (int i = 0; i < sizeof(selectors)/sizeof(selectors[0]); i++) {
    [self performSelector:selectors[i]];
    // ....
    }

    ReplyDelete
  4. You can also create an array of NSInvocations. This is handy if you need an argument to go with your selector.

    NSMethodSignature *sig = [[yourTarget class] instanceMethodSignatureForSelector:yourSEL];
    NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
    [inv setTarget:yourTarget];
    [inv setSelector:yourSEL];
    [inv setArgument:&yourObject atIndex:2]; // Address of your object

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.