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 555007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:46:15+00:00 2026-05-13T11:46:15+00:00

I’m new to the Objective C business (Java developer most of the time) and

  • 0

I’m new to the Objective C business (Java developer most of the time) and am woking on my first killer app now. 🙂
At the moment I am somehow confused about the usage of selectors as method arguments. They seem to be a little bit different than delegates in C# for example.

Given the following method signature

-(void)execute:(SEL)callback;

is there a way to enforce the signature for the selector passed to such a method?
The method is expecting a selector of a method with the following signature

-(void)foo:(NSData*)data;

But the SEL (type) is generic, so there is a good chance to pass a wrong selector to the
execute method. OK at least at runtime one would see a funny behavior… but I would like to see a compiler warning/error when this happens.

  • 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-13T11:46:15+00:00Added an answer on May 13, 2026 at 11:46 am

    The quick answer is: no, there is no way to have the compiler enforce the method signature of a method selector that is provided via a SEL argument.

    One of the strengths of Objective-C is that it is weakly-typed language, which allows for a lot more dynamic behaviour. Of course, this comes at the cost of compile-time type safety.

    In order to do what (I think) you want, the best approach is to use delegates. Cocoa uses delegates to allow another class to implement “callback”-type methods. Here is how it might look:

    FooController.h

    @protocol FooControllerDelegate
    @required:
    - (void)handleData:(NSData *)data forFoo:(FooController *)foo;
    @end
    
    @interface FooController : NSObject
    {
        id <FooControllerDelegate> * delegate;
    }
    @property (assign) id <FooControllerDelegate> * delegate;
    - (void)doStuff;
    @end
    

    FooController.m

    @interface FooController (delegateCalls)
    - (void)handleData:(NSData *)data;
    @end
    
    @implementation FooController
    
    @synthesize delegate;
    
    - (id)init
    {
        if ((self = [super init]) == nil) { return nil; }
        delegate = nil;
        ...
        return self;
    }
    
    - (void)doStuff
    {
        ...
        [self handleData:data];
    }
    
    - (void)handleData:(NSData *)data
    {
        if (delegate != nil)
        {
            [delegate handleData:data forFoo:self];
        }
        else
        {
            return;
            // or throw an error
            // or handle it yourself
        }
    }
    
    @end
    

    Using the @required keyword in your delegate protocol will prevent you from assigning a delegate to a FooController that does not implement the method exactly as described in the protocol. Attempting to provide a delegate that does not match the @required protocol method will result in a compiler error.

    Here is how you would create a delegate class to work with the above code:

    @interface MyFooHandler <FooControllerDelegate> : NSObject
    {
    }
    - (void)handleData:(NSData *)data forFoo:(FooController *)foo;
    @end
    
    @implementation MyFooHandler
    - (void)handleData:(NSData *)data forFoo:(FooController *)foo
    {
        // do something here
    }
    @end
    

    And here is how you would use everything:

    FooController * foo = [[FooController alloc] init];
    MyFooHandler * fooHandler = [[MyFooHandler alloc] init];
    ...
    [foo setDelegate:fooHandler]; // this would cause a compiler error if fooHandler
                                  // did not implement the protocol properly
    ...
    [foo doStuff]; // this will call the delegate method on fooHandler
    ...
    [fooHandler release];
    [foo release];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.