Why is init not a class method? - objective-c

Why is the init method not a class method? I mean init's method body starts with an -.
Methods starting with - are instance methods as far as I know, but obviously we want to create an instance.

init is not for creating an instance; that's alloc's job (and alloc is a class method).
init is for setting up the created instance. It needs access to the new instance's ivars, and must be an instance method.

Related

Why is -init an instance method and +initialize a class method? [duplicate]

This question already has an answer here:
Why is init not a class method?
(1 answer)
Closed 8 years ago.
In Cocoa, for NSObjects, shouldn't both init and initialize be class methods?
+initialize can be overridden (it's optional) to perform class-wide initialization.
-init performs initialization of a single instance of a class, though it's often refined by adding arguments in classes derived from NSObject (ex: UIView's initWithFrame: method).
Since -init initializes a single instance (in particular, it has access to the instance's variables), it can't be a class method.
From the docs:
The runtime sends initialize to each class in a program just before the class, or any class that inherits from it, is sent its first message from within the program.
This means that the first time you send a message to the class, whether it be alloc or some defined class method, initialize is called first, once, for the entire run of the application. As opposed to load, it is possible to include a class in a project and never hit initialize.
init, on the other hand, is
Implemented by subclasses to initialize a new object (the receiver) immediately after memory for it has been allocated.
Meaning, init is sheerly used for initializing class instances.
Edit --
Following the edited question, alloc creates the instance while init initializes it, which is why alloc is a class method and init is an instance method.

What's the difference of instance method and class method in Objective-C? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Objective-C: Class vs Instance Methods?
Objective-C - difference between class method and static method?
In ObjC, A single dash before a method name means it's a instance method. A plus before a method name means it's a class method. but what is the difference in programming?
The difference between a class method and an instance method is that an
instance method requires an instance of the class on which it will
(generally) operate. The message to invoke an instance method must be
sent to an instance of a class.
Probably the most common single use of class methods is object
factories; messages that you send to a class to create an instance
configured according to the parameters you've sent in. For example in
Cocoa the NSString class has several class methods named
stringWithSomethingOrOther: that will create a new NSString object and
hand it back to you.
On the other hand, NSString also has many instance methods -
operations which really have no meaning without an actual instance to
work with. A commonly-used one might be the length method, which tells
you how many characters are in the specific NSString instance to which
the message is sent.
Also see this.
What is the difference between class and instance methods?
An instance method is invoked on objects. A class method is invoked on class.
For example the line:
SomeClass *object = [[SomeClass alloc] init];
Here you can see that the "alloc" works on "SomeClass" and not on "object".
Whereas:
[object callMyFunction]; will act on "object" and not "class". This is an instance method.
The main difference with those two is the former one ie with single dash before it is only called by the instance of that class where it is declared ie one have to create one instance of that class means one object for that class and using . one can call the instance method
In class method, the later one can be called directly using the class name. To call class methods one dosen't need any object.
Please refer this link from apple developers documents

Ok to skip init method in inherited class?

Consider the following text in http://developer.apple.com/library/ios/#documentation/general/conceptual/CocoaEncyclopedia/Initialization/Initialization.html.
Inherited initializers are a concern when you create a subclass. Sometimes a superclass init... method sufficiently initializes instances of your class. But because it is more likely it won’t, you should override the superclass’s initializer. If you don’t, the superclass’s implementation is invoked, and because the superclass knows nothing about your class, your instances may not be correctly initialized.
On the same page I find this text:
Every object that declares instance variables should implement an initializing method—unless the default set-everything-to-zero initialization is sufficient.
My question is:
If I skip the init method in class B, where class B inherits from A, can I trust that B's non-inherited member variables are set to zero?
My question is: If I skip the init method in class B, where class B
inherits from A, can I trust that B's non-inherited member variables
are set to zero?
Objective-C will set all ivars of any new object to zero:
The alloc method dynamically allocates memory for the new object’s
instance variables and initializes them all to 0—all, that is, except
the isa variable that connects the new instance to its class. For an
object to be useful, it generally needs to be more completely
initialized. That’s the function of an init method.
So it's okay to skip implementing an initialization method for your class if you don't have any ivars/properties that need to be initialized. You must, of course, still initialize new objects by calling -init or some other initialization method so that the superclass has an opportunity to initialize itself.
Yes, Class B's non-inherited member variables will be zero. Inherited variables will have whatever value is set in Class A's init method (or zero if not set).

When do we create memory for ivars in Singleton example by Apple?

I have seen the Apple's example of Singleton and couple of other examples.
People say that it is too strict!
But the point is ..even if it is too strict, I want to understand it.
I dont understand that when we call allocWithZone on super, What happens ?
Memory will be created according to super's instance size.
What if our Singleton has ivars ?
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32
I also dont understand that, Why does allocWithZone return the object with a retain call when retain itself is returning the object as it is.
Whenever a method is called in Obj-C it is passed an object reference via the hidden parameter self. For an instance method self refers to the object the method was invoked on, for a class method self refers to the the class object (of type Class) the method was invoked on. Calls to super implicitly pass on self.
Therefore in Apple's example code the call [super allocWithZone:NULL] calls the super implementation of allocWithZone passing the current value of self, which is MyGizmoClass's class object as it is a static method. The implementation of allocWithZone can determine the required memory size from the passed Class object – the details of how are private.
As you've correctly spotted, the call to retain in allocWithZone is pointless but harmless.

Accessing Singleton Instance Variable in Class Methods Throws Warning?

I've using the Objective-C singleton from here at stackoverflow.
The singleton in the class method accesses it's instance variable, which works, but throws a complie warning. How should I be doing this? Is there a way to do this without accessing the sharedInstance: in each class method?
for example here is my class method:
+ (NSString *)myClassMethods {
[instanceDateFormatter setFormat:#"MM"];
return [instanceDateFormatter stringWithDate:somedate];
}
line 2 will have the complie warning.
Thanks,
Ross
You should be using the sharedInstance: call in each class method. I guess if you really want to, you could work around it with global variables, but the right solution is as you mentioned.
Since instanceDataFormatter is an instance variable, you have to access it via a class instance -- so you'll need to go through your sharedInstance method to get it. Or, you could access it via the static singleton variable, bypassing the call to sharedInstance (however, that could break if the static variable hasn't been initialized yet).