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

  • SEARCH
  • Home
  • 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 7189275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:16:04+00:00 2026-05-28T19:16:04+00:00

I came across this code in ShareKit, and I don’t understand what the writer

  • 0

I came across this code in ShareKit, and I don’t understand what the writer had in mind, using self in a class method. There is a warning: Incompatible pointer types sending ‘Class’ to parameter type id<FBSessionDelegate>. I want to clean up these warnings, so I can see the ones that may hurt later. What can/should I do that won’t break this?

This is is the file SHKFacebook.m and the class name is SHKFacebook

+ (void)logout
{
    FBSession *fbSession; 

    if(!SHKFacebookUseSessionProxy){
        fbSession = [FBSession sessionForApplication:SHKFacebookKey
                                                 secret:SHKFacebookSecret
                                               delegate:self];

    }else {
        fbSession = [FBSession sessionForApplication:SHKFacebookKey
                                        getSessionProxy:SHKFacebookSessionProxyURL
                                               delegate:self];
    }

    [fbSession logout];
}
  • 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-28T19:16:05+00:00Added an answer on May 28, 2026 at 7:16 pm

    self may be used in a class method as a polymorphic Class instance.

    therefore, the class method new can be implemented like this:

    + (id)new
    {
      return [[self alloc] init];
    }
    

    and would return the correct type for the Class instance that is messaged:

    ex a:

    NSArray * a = [NSArray new]; // << a is an NSArray
    

    ex b:

    NSMutableArray * a = [NSMutableArray new]; // << a is an NSMutableArray
    

    see note below.

    So what you are really faced with is ensuring there are only instance methods in the protocol, and that (Class) self’s methods map out to adopt the instance methods in the protocol.

    As far as the design… well, let’s just say I would not have written it this way. A singleton would have been clearer and more correct, but I don’t even like singletons so I would not have taken that route.

    The warning is produced because the Class instance (what is passed) does adopt the @protocol specified by the delegate parameter. The Class instance is not an instance of the class. A protocol declaration really applies to instances of the class. For example, if you adopt NSLocking, does the compiler expect you to also implement class methods for every instance method declared in the protocol? Answer: Never. The implementation you’re dealing with is IMO one of those cases where it’s a misuse of the language, but it happens to work.

    To clarify the terminology:

    The “Class instance” is self in a class method:

    + (void)foo { self; }
    

    An “instance of the class” is self in an instance method:

    - (void)foo { self; }
    

    In practice, -[NSObject conformsToProtocol:] is +[NSObject conformsToProtocol:] and +[NSObject class] just returns self so there are no errors at execution.

    I’m still not clear, then, why I am receiving a warning, if the code meets the criteria you described.

    The criteria I described applies to the execution, but it deviates from the semantics of the language — thus, the compiler is absolutely correct on this.

    For a resolution to the problem: There’s no way to tell the compiler “My Class instance conforms to protocol” because declaration of adoption applies to instances of the class.

    You have two primary options:

    1. The clean, correct approach: Use an instance of the class and implement the protocol as defined.

    2. or Typecast the class instance to the protocol:

      id delegate = (id)self;
      fbSession = [FBSession sessionForApplication:SHKFacebookKey
      getSessionProxy:SHKFacebookSessionProxyURL
      delegate:delegate];

    If you choose #2, it may help to define the instance methods of the protocol to use class methods, like so:

    + (void)protocolMethod { /* do stuff */ }
    - (void)protocolMethod { [self.class protocolMethod]; }
    

    that would also imply you never need instances. It would help because it will add warnings if the protocol will change. These warnings would bubble up to class methods when you follow the convention.

    To reduce noise, you may also consider creating some method to reduce the typecast to one location:

    + (id<SomeProtocol>)sharedSomeProtocolDelegate
    {
      return (id<SomeProtocol>)self;
    }
    
    - (id<SomeProtocol>)sharedSomeProtocolDelegate
    {
      return [[self class] sharedSomeProtocolDelegate];
    }
    

    then you can just write:

    fbSession = [FBSession sessionForApplication:SHKFacebookKey
                                 getSessionProxy:SHKFacebookSessionProxyURL
                                        delegate:[self sharedSomeProtocolDelegate]];
    

    (note that the implementations of these types are actually class clusters, and you will see something different in the debugger or if printed)

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

Sidebar

Related Questions

I came across this code today and don't really understand it. Please could someone
Yesterday I read some code of a colleague and came across this: class a_class
While writing some code i came across this issue: #include <iostream> class random {
I came across this strange code snippet which compiles fine: class Car { public:
I came across this code and need to understand what it is doing. It
I came across this code in a php database class: if( !$this->_Link_ID ) Link_ID
I came across this code of Reset(elfhandle,1) and I don't know what the second
Im creating a model in qt and came across this code: class StringListModel :
I came across this code today AsyncInvoke(OnTimeMessageTimer, (object)null, (ElapsedEventArgs)null); Is there anything wrong with
I just came across this code and a few Google searches turn up no

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.