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')

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

5 Answers5

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 :).

endy
  • 3,872
  • 5
  • 29
  • 43
  • Thanks, you convinced me not to go that way :) – Gal May 15 '12 at 14:27
  • 2
    @Gal: it's actually a reasonable pattern and not hacky at all. – JeremyP May 15 '12 at 15:02
  • 1
    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`. – jlehr May 15 '12 at 15:20
  • @endy someone has copied your answer on http://stackoverflow.com/questions/31426411/how-to-call-non-static-method-from-static-method-in-objective-c/31426473#comment50857429_31426473 claimed it as their own. – Popeye Jul 16 '15 at 07:08
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.

Vignesh
  • 10,205
  • 2
  • 35
  • 73
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.

allaire
  • 5,995
  • 3
  • 41
  • 56
  • 1
    Objective-C does not have static methods. It has class methods. They are quite different. – bbum May 15 '12 at 14:19
  • Right, didn't notice the objective-c tag, sorry. But like this good post says: http://stackoverflow.com/a/8089623/277370 -- In day to day coding, there's not much difference, but a couple of concepts are still different, sorry. – allaire May 15 '12 at 14:21
  • 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). – bbum May 15 '12 at 16:14
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.

Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304
  • 1
    Objective-C does not have static methods. It has class methods. They are quite different. – bbum May 15 '12 at 14:19
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.

Michał Zygar
  • 4,052
  • 1
  • 23
  • 36
  • 1
    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. – jlehr May 15 '12 at 15:26
  • 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. – Michał Zygar May 16 '12 at 04:46
  • 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. – jlehr May 16 '12 at 13:13
  • 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. – Michał Zygar May 16 '12 at 19:52
  • 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. – jlehr May 16 '12 at 23:21