4

How can I call a non-static method from a static method in same object?

Inside the static method: if I use [ClassName nonStaticMethod] Or If I use [self nonStaticMethod] I get warning: Class method '+isTrancparentImageWithUrl:' not found (return type defaults to 'id')

3
  • 5
    You need to create an instance. I believe they are called class/instance methods not static/non-static in Objective-C Commented May 15, 2012 at 14:14
  • 2
    Exactly. Objective-C does not have static methods. It has class methods. They are quite different. Commented May 15, 2012 at 14:19
  • So many good answers and I can only accept 1... Thank you all. Commented May 15, 2012 at 14:32

5 Answers 5

7

One solution (and highly controversial) solution is to convert you class into a singleton implementation and then convert all static methods into regular methods.

EG if you had a class called FileManager and in there you had a method which looked like

+ (NSString *) getDocumentsDirectory

and for whatever reason you wanted to call a nonstatic method from inside there you would need to change your implementation to be something like this

+ (FileManager *)sharedInstance {
    // Singleton implementation
    static FileManager* instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[FileManager alloc] init];
    });
    return instance;
}

- (NSString *) getDocumentsDirectory

And instead of calling

[FileManager getDocumentsDirectory]; 

you would call

[[FileManager sharedInstance] getDocumentsDirectory];

There are several reasons as to why you wouldn't want to create a singleton, however, that is beyond the scope of my response :).

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, you convinced me not to go that way :)
@Gal: it's actually a reasonable pattern and not hacky at all.
A couple of things: 1) there's no such thing as a static method in Objective-C; and 2) by convention, getter methods are never prefixed with the word get.
@endy someone has copied your answer on stackoverflow.com/questions/31426411/… claimed it as their own.
3

You need to create the object of the class to call non class methods, You need an instance to call such methods that is why those methods are called instance methods.

calling [self instanceMethod] from class method wont work, because self in class method points to the class rather any instance. Here you can find information the on use of self in class methods.

Comments

1

You can't

You can call static method/variables in instance methods, but not the other way.

Reason is simple, the static methods are binded to the class, not the instances (objects) of the class.

3 Comments

Objective-C does not have static methods. It has class methods. They are quite different.
Right, didn't notice the objective-c tag, sorry. But like this good post says: stackoverflow.com/a/8089623/277370 -- In day to day coding, there's not much difference, but a couple of concepts are still different, sorry.
As long as your day to day doesn't involve subclassing, I agree that it doesn't make a huge difference. As soon as you get into API design or implementing a class hierarchy, it makes a huge difference (see Class Clusters).
0

You can create an instance of the current class and then call it on that, but it's not recommended to do that. Static methods can't call non-static ones - there is no 'this' in a static context.

1 Comment

Objective-C does not have static methods. It has class methods. They are quite different.
0

There is no static method in Objective-C. If you want to call [Class method] it is called class methods(not static in terms of ANSI C)

However, you may find Singleton pattern useful : http://cocoadev.com/wiki/SingletonDesignPattern

(basically, you hold static shared instance of an object)

edit: [ClassName instanceMethod] - nonStaticMethod is not defined for ClassName. It is defined for ClassName objects(instances), so you cannot use it(it doesn't exist), app may crash.

[self instanceMethod]- you cannot use that either, because when calling class method, there is no self-because there's no object(instance). You may workaround it, using singleton pattern i posted above.

5 Comments

Objective-C doesn't have static methods, it has class methods. They're dynamically dispatched by the same mechanism used to dispatch instance methods, and they're inherited (and can be overridden) by subclasses, so there's nothing static about them.
Yes, you are right, but for someone coming from another language it is easier to use this analogy, which is understandable, although not 100% correct.
It's not less than 100% correct -- it's wrong, unless you clearly state that you're drawing an analogy. And even then I'd argue that it's a misleading analogy. The analogue of static methods in Objective-C is C functions, not class methods.
Well, I'm not the one using analogy here- I meant OP. And in my opinion it's good. I can see that you are rather C-based programmer than i.e. java-based programmer. I believe when someone comes from java to obj-c and asks for static method, class method is what he means.
I believe one of our responsibilities in answering questions on Stackoverflow is to point out incorrect assumptions on the part of other posters. I'm not sure what you meant by 'C-based programmer', but I've actually written a couple of books on Java development, so I'm not completely unfamiliar with the language. Again, static methods are a feature of Java, not Objective-C, and it would be misleading at best not to point that out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.