Ok to skip init method in inherited class? - objective-c

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

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.

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.

Where should I set default variable values in ObjectiveC class

I can't set default values in the header file, so where should they be set? In the class's designated initialiser?
Yep, if you need specific values, use the init like methods.
This is the recommended way.
The designated initializer is the method in each class that guarantees inherited instance variables are initialized (by sending a message to super to perform an inherited method). It’s also the method that does most of the work, and the one that other initialization methods in the same class invoke. It’s a Cocoa convention that the designated initializer is always the method that allows the most freedom to determine the character of a new instance (usually this is the one with the most parameters, but not always).

Class methods and instance methods - when/when not to use them?

I was wondering when and when not to use class methods and instance methods. I need some practical examples. I am really confused. Another question: can't we do exactly the same things with instance methods that we can with class methods?
Class methods: The method isn't tied to any specific object. In a way it acts like a free function in the class's namespace. No 'self' pointer. For instance [UIScreen mainScreen] is a class method because there's only one screen and there's no need to care about multiple 'screen instances'.
Instance method: Tied to a specific object.
This applies to most OO languages, not just obj-C.
At the implementation level, an instance method call contains a hidden pointer to a data structure (the object), a class method does not.
The practical question to ask is whether your call requires sending the call some specific data which is or could best be encapsulated as instance data inside an object, or not.
You (usually) can do the same thing with class methods as instance methods, but then you have to explicitly pass the object as a visible parameter in the call, which is uglier looking and also potentially disables some method override features of the Objective C language.
Use class methods for utility functions and Instance methods for object oriented stuff.
Eg. For Mathematical calculation (eg sin x ) use class method. But for invoking a behavior specific to an object.. use instance method ..
A class method as the name implies is bounded to the class. You can invoke them just with the name of the particular class. These can be normally exposed methods of a class.
For example
NSArray
+ (id)arrayWithArray:(NSArray *)array;.
You call it with the class name NSArray. What you expect is just a creation of a object of the type of that particular class. This doesn't need an object to invoke. Also these are very basic method required so its better to make it as a class method.
On the other hand instance method as the name implies is very much bound to the instance. Object is an entity that encapsulates state (ivars) and behaviors (methods) of a class. This can be very specific to the object.
For example
- (NSUInteger)count;
Lets take NSArray *a and NSArray *b. If a contains 5 items whereas b contains 4, instance methods called upon these instances will produce different results. And thats why we need instances to be initialized while invoking instance method. They work on the context(or state) of the object they are been called upon. Also they are not exposed as the class methods are.
Hope this helps.
If you want to use instance objet or instance variable you have to go with instance Methods.
Bcz Inside the class you cant access the Instance instance objet or instance variable.
Class methods are static methods.

Difference between Obj-C object and instance?

I am learning Objective-C and I don't really understand the difference between an instance and an object. They commonly use both terms. Also, why is the alloc method applied to the class and the init method applied to the object?
An instance is the same as an object, hence why both terms are used and can be used interchangeably — an instance of NSString, or an NSString object.
+alloc is a class method responsible for allocating the memory needed to store a new instance/object, and zeroing-out that memory. It returns a newly created instance/object.
-init, on the other hand, is responsible for further initialising this new instance;1 for example, assigning default values to instance variables. Since -init operates on an existing instance, it’s an instance method.
1In some cases -init might return an instance different from the one created by +alloc.