Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6111309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:36:57+00:00 2026-05-23T14:36:57+00:00

I am quite new to blocks and objective-c, and i am trying to write

  • 0

I am quite new to blocks and objective-c, and i am trying to write my first category using both. My idea is to create a category on NSTimer that will receive a block as a parameter and this block will be used in the selector call. Right now I have this.

// NSTimer+Additions.h

#import <Foundation/Foundation.h>

typedef void (^VoidBlock)();

@interface NSTimer (NSTimer_Additions)


+ (NSTimer *)scheduleTimerWithTimeInterval:(NSTimeInterval)theSeconds repeats:(BOOL)repeats actions:(VoidBlock)actions;
@end

#import "NSTimer+Additions.h"

static VoidBlock _voidBlock;

@interface NSTimer (AdditionsPrivate) // Private stuff
- (void)theBlock;
@end


@implementation NSTimer (NSTimer_Additions)


+ (NSTimer *)scheduleTimerWithTimeInterval:(NSTimeInterval)theSeconds repeats:(BOOL)repeats actions:(VoidBlock)actions {

    [_voidBlock release];
    _voidBlock = [actions copy];

    NSTimer* timer = [[NSTimer alloc] initWithFireDate:[NSDate date] 
                                          interval:theSeconds
                                            target:self 
                                          selector:@selector(theBlock) 
                                          userInfo:nil 
                                           repeats:repeats];
    [timer fire];

    return [timer autorelease];
}


- (void)theBlock {
    _voidBlock();
}

@end

Gist for the code: https://gist.github.com/1065235

Everything compiles fine but i have the following error:

2011-07-05 14:35:47.068 TesteTimer[37716:903] * Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘+[NSTimer theBlock]: unrecognized selector sent to class 0x7fff70bb0a18’

How can I make this category work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:36:58+00:00Added an answer on May 23, 2026 at 2:36 pm

    Your major flaw besides the wrong target is your use of a static variable. You won’t be able to support beyond a single timer.

    Using block as argument to the invoked method.

    @interface NSTimer (AdditionsPrivate) // Private stuff
    - (void)theBlock:(VoidBlock)voidBlock;
    @end
    
    
    @implementation NSTimer (NSTimer_Additions)
    
    + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)theSeconds repeats:(BOOL)repeats actions:(VoidBlock)actions {
        NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:[self instanceMethodSignatureForSelector:@selector(theBlock:)]];
        NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:theSeconds
                                                       invocation:invocation
                                                          repeats:repeats];
        [invocation setTarget:timer];
        [invocation setSelector:@selector(theBlock:)];
        
        Block_copy(actions);
        [invocation setArgument:&actions atIndex:2];
        Block_release(actions);
    
        return timer;
    }
    
    
    - (void)theBlock:(VoidBlock)voidBlock {
        voidBlock();
    }
    
    @end
    

    The problem with using associative references was the leak as there was no good point to release the block.


    Earlier Approach using associative references

    You can use associative references to attach the block to that particular instance of NSTimer.

    @implementation NSTimer (NSTimer_Additions)
    
    + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)theSeconds repeats:(BOOL)repeats actions:(VoidBlock)actions {
        NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:[self instanceMethodSignatureForSelector:@selector(theBlock)]];
        NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:theSeconds
                                                       invocation:invocation
                                                          repeats:repeats];
        [invocation setTarget:timer];
        [invocation setSelector:@selector(theBlock)];
    
        objc_setAssociatedObject(timer, @"Block", actions, OBJC_ASSOCIATION_COPY);
        
        return timer;
    }
    
    
    - (void)theBlock {
        VoidBlock _voidBlock = (VoidBlock)objc_getAssociatedObject(self, @"Block");
        _voidBlock();
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quite new to maven here so let me explain first what I am trying
Im quite new to spring mvc. What I'm trying to achive is separating static
Iam quite new to functions in SQL and I would like to create a
Am quite new to namespacing etc so hopefully this will be straightforward. I have
I wrote a test to experiment with RestKit using blocks. I thought that the
I'm quite new to Django and I just set up my first registration page
I'm new to Objective-C and my C/C++ skills are quite rusty. What better time
For quite a long time I've wanted to start a pet project that will
Being quite new to rails and currently building a project, i'm begining to be
Scenario: Quite new to DI and Ninject but would love to master it so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.