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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:30:43+00:00 2026-06-05T03:30:43+00:00

I’m quite a newbie in Objective C, though I have some background in Java

  • 0

I’m quite a newbie in Objective C, though I have some background in Java reflection.

Here, I have a classic class method findAll that find all the domain objects from the database. The class Univers directly inherits from DomainObject

@interface DomainObject : NSObject

  - (NSString *) execute : (NSString*) method  withJson:(NSString*)json;
  + (NSString*)findAll: (NSString*)json;

@end

@implementation DomainObject

  - (NSString *) execute: (NSString*) method  withJson:(NSString*)json{

      method = [NSString stringWithFormat:@"%@%@", method, @":"]; 
      //method is 'findAll:'
      NSString* result =  [ self performSelector: 
          NSSelectorFromString(method) withObject:json];// Error here
      return result;    
  }
@end

The code was working when findAll was NOT a class method (ie -findAll declaration), but now I have the error : NSInvalidArgumentException -[Univers findAll:]
It clearly seems that the runtime is looking for an instance method.

Any idea to find my class method ?

  • 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-06-05T03:30:44+00:00Added an answer on June 5, 2026 at 3:30 am

    Instead of calling

    NSString* result =  [self performSelector:NSSelectorFromString(method) withObject:json];
    

    you need to call

    NSString* result =  [[self class] performSelector:NSSelectorFromString(method) withObject:json];
    

    for class methods.

    After all it’s the object instance’s class that supposed to be calling the method, not the instance itself.

    Short explanation: NSObject implements - (Class)class; (not to be mistaken with + (Class)class of similar effect, which NSObject implements, too!) which returns the Class object of your instance object. Keep in mind that in Objective-C in addition to plain instance objects, Classes are actual objects, too: objects of type Class, that is (vs. id, NSObject, …).

    See the documentation for the -class method here.


    Btw, you should probably wrap your method call into an conditional block to prevent exceptions caused by calls to missing methods.

    SEL selector = NSSelectorFromString(method);
    if ([[self class] respondsToSelector:selector]) {
        NSString* result =  [[self class] performSelector:selector withObject:json];
    }
    

    In general it’s a common pattern in Objective-C to call an object’s class method by receiving the class object via [object class].

    Consider this case of a class called Foo implementing a convenience method for returning an autporeleased instance of itself (to be called via: Foo *newFoo = [Foo foo];):

    While it would certainly be possible to implement said method like this (after all we know the object’s class name, right?):

    + (id)foo {
        return [[[Foo alloc] init] autorelease];
    }
    

    the correct way is this:

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

    As the first one would cause problems with polymorphism in subclasses (Such as a subclass called FooBar, for which it should clearly be [FooBar alloc] …, not [Foo alloc] …. Luckily [[self class] alloc] solves this dynamically).
    While this is clearly not the right place for a thorough explanation of this (rather offtopic one might say) it’s certainly worth noting/warning about, imho.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.