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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:22:00+00:00 2026-05-20T10:22:00+00:00

The Situation My custom controller class has the following method: – (void)requestViewControllerWithIdentifier:(NSString *)identifier fromObject:(id)object;

  • 0

The Situation

My custom controller class has the following method:

- (void)requestViewControllerWithIdentifier:(NSString *)identifier fromObject:(id)object;

This causes object to receive this message:

- (UIViewController *)viewControllerWithIdentifier:(NSString *)identifier;

The Problem

Right now, object is just an id so there’s no guarantee that it actually implements that method, and I get a compiler warning when I send the message.


The Solution

I came up with two solutions myself:

  1. Use a protocol.
  2. Make id an NSObject and create a category for NSObject.

They are both fine solutions probably and I don’t mind choosing one of them, but…


The Question

…I noticed Apple is doing something odd in their GameKit API. GKSession has the following method:

- (void)setDataReceiveHandler:(id)handler withContext:(void *)context

handler is just an id, but Apple actually requires it to implement this method:

- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context;

Without making use of any protocol or category! I’m wondering how and why would they do this? Why don’t use a protocol? Do they enforce the method in some other way? If I were to do this, how can I suppress the compiler warning?

  • 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-20T10:22:00+00:00Added an answer on May 20, 2026 at 10:22 am

    You can assign a variable of type id to any object type. If you know that it must implement a given protocol you can assign it to a variable of that type within the method and invoke the method on that variable.

    As a design point, I would say that it is better to make the protocol explicit and externalise it to the caller so that the compiler can do type checking properly. Some parts of Apple’s code are better than others at this: from what you say GameKit is very much at the unhelpful end of things.

    Defining a category is not what you want to do, because that tells the compiler that you will add the method to every NSObject.

    If you have this protocol:

    @protocol YourProtocol <NSObject>
    - (UIViewController *)viewControllerWithIdentifier:(NSString *)identifier;
    @end
    

    And define your method as:

    - (void)requestViewControllerWithIdentifier:(NSString *)identifier fromObject:(id <YourProtocol>)object;
    

    It will probably do what you want. It may not be strictly necessary in this case but it’s usually a good idea to have your protocol extend the NSObject protocol (as above) so that you can call useful stuff like -retain, -release, -autorelease, and -respondsToSelector. The alternative of declaring the method as follows prevents the user from using an NSProxy-rooted object as the object parameter.

    - (void)requestViewControllerWithIdentifier:(NSString *)identifier fromObject:(NSObject <YourProtocol> *)object;
    

    If it’s just for your own use and you aren’t using proxy objects this can be quite convenient, but you should avoid it in public APIs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my situation is like this thread : ASP.NET Custom Validator + WebMethod + jQuery
I have the following situation: There is one custom view inside of the first
Well, here's the situation: I've got... a custom class with an UIImageView -property, let's
I have the following situation: taglib.jar : A custom JSP taglib packaged as a
I have following situation: I have a custom ListView with ImageView and TextView in
I'm currently following along with this railscast and for my specific situation am running
Please, give me a hint how to solve this situation: I have two custom
Situation: A custom Master Document content type inherits from Document The Master Document content
Situation: Drupal 6 node module defining custom content type Necessity to have an automated
My situation: an activity with a button, a checkbox checkAll and a custom ListView

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.