How to access self in class method objective C

class-method, ios, objective-c

Solution

The whole point of a class method is that it is not part of a specific instance. Inside of a class method, `self` is the class.

If you need to be tied to a specific instance, then it should be an instance method. If you want a static method that accesses a specific instance, then pass that instance (`self`) to it (though it's hard to imagine many cases where that makes sense).

In the above example, `showHUD` should be an instance method almost certainly. If that doesn't make sense for some reason, then it should be:

+ (void)showHUDForWindow:(UIWindow *)window;

You can then call it as `showHUDForWindow:self.window` and use that as needed.

Problem

I havea Utility class that uses class methods. I am trying to refer to self in the class method but can't. I was wondering how would I declare the following in a class method: ``` [MRProgressOverlayView showOverlayAddedTo:self.window animated:YES]; ``` `self.window` it says member reference type `struct objc_class *' is a pointer; maybe you meant to use '->'` Another problem that relates to not being able to call `self` is how would I refer to a declared `@property` in my `.h` in a class method in my `.m`. Here is my class method: ``` .m + (void)showHUD { [UIApplication sharedApplication].networkActivityIndicatorVisible=YES; [MRProgressOverlayView showOverlayAddedTo:self.window animated:YES]; //I would preferably like to call my property here instead } .h @property (nonatomic) MRProgress * mrProgress; ```

Original source