1

I'm afraid this is a stupid question, but I honestly don't understand.

I have a class, RemoteConfiguration and currently it's functionality is all instance methods. This being so, I have to use it like:

RemoteConfiguration* configs = [[RemoteConfiguration alloc] init];
[configs updateSettings];
[configs release];

This is annoying because there is no need to create an object just to use one method which could be a class method and called like:

[RemoteConfiguration updateSettings];

But when I change my methods from - to + Xcode complains about every object I access with self, suggesting that I use -> instead. (Which also causes a warning) I don't get this. Regardless of the methods, the object will still have its member variables. So why is -> necessary? And how can I use member variables in class methods?

user
  • 3,388
  • 7
  • 33
  • 67

3 Answers3

5

Within a class method, the keyword self doesn't refer to an instance of that class type, but rather the actual class instance, as in the result of [RemoteConfiguration class].

Class instances like this do not have instance variables (at least not the ones you're declaring, but rather the Class instance variables, most notably the isa instance variable).

If you wish to have a single static instance of RemoteConfiguration you can declare the instance variables as static variables within the .m file, or use something like the singleton pattern to keep an instance handy.

Community
  • 1
  • 1
Ben S
  • 68,394
  • 30
  • 171
  • 212
1

Without seeing the code it sounds like you are referencing self from within the method that you want to be a class method.

You cannot reference self because you are performing an action on the class, not an instance of the class. Hence self does not refer to what expect.

You'll have to modify the method if you truly seek it to be a class method. Keep in mind, you'll only have local variables and static variables to work with. If you have any properties or instance variables you wont have access to those either.

feliun
  • 181
  • 6
  • See Benoit's answer as its a little more detailed then my own. And he goes into the singleton pattern which will definitely help – feliun Jul 08 '13 at 18:08
-4

“self” points to the instance. A class method does not have any instance associated with it.

Basically, if you call a class method, “the object” that “will still have its member variables” is not “seen” by the method, because it’s a method on the class as a whole, not on specific individual objects.

You can probably pass one to it – but then, why use a class method in the first place? (In short: don’t.)

There’s also the possibility to use class variables instead of instance variables, in theory… it all depends.

mirabilos
  • 5,123
  • 2
  • 46
  • 72
  • This is incorrect. `self` inside of a class method refers to the `class` instance. For example, `[[self alloc] init]` is valid code inside a class method. – Ben S Jul 08 '13 at 15:57
  • I think alloc is somewhat special here, as it requires the class. His example, on the other hand, refers to *instance* variables. – mirabilos Jul 08 '13 at 16:08
  • 2
    @mirabilos there is absolutely nothing special about +alloc. Class methods have a `self` and that `self` refers to the class as Benoit said. – bbum Jul 08 '13 at 18:34
  • Yes, but that’s precisely what I was saying: the “self” of a class method is not the same as the “self” of an instance method, which is an object, not the class itself. I admit being a bit confused about alloc, yes it’s not special. – mirabilos Jul 09 '13 at 08:52