In what circumstances do i need my own initializes to initialize object? - objective-c

I know objective c has built in 'init' method to initialize object. But i want to know In what circumstances do i need my own initializes to initialize object?
I want to know what value is assigned to self when i invoke 'self = [super init]'

You only need your own initializer when you need your object to default to a state that is different than what the compiler will enforce. That is, all instance variables are initialized to 0 (for integer-like types) or 0.0 (for floating point types). That means all pointers are nil and all numbers are 0(.0). If you need to initialize anything to a different value (creating internal container objects, for example), you need your own initializer.
Similarly, if your class inherits from a class that implements its own initializer, you only need your own if you want your state to be different than the superclass's.

Related

What is the Purpose of Multiple Initializers

I understand the purpose of having an initializer is to set the instance variables to certain values. However, I am confused as to why you would want to have multiple initializers for each instance variable. Can't one initializer set all the instance variables? I guess my question really is what is the practicality of having multiple initializers in a class. For instance a class has the following:
- (id)initWithItemName:(NSString *)name
{
return [self initWithItemName:name valueInDollars:0 serialNumber:#""];
}
Meanwhile the designated initializer is given as such:
- (id) initWithItemName:(NSString *)name valueInDollars:(int)value
serialNumber:(NSString *)sNumber;
The designated initializer, as you can see, already initializes the itemName variable. So, whats the pont of having a specific initializer just for one variable.
Default values. If you use the designated initializer with a default value that you have to specify each time, it can be a pain if you want to change this default value because you would have to go to every instance of this initializer and change the value. If you have an initializer that centralizes the location of the default value, it's easy to change.
Deprecation. If you have to add a variable to the class and to the initializer later on, then it's much easier to not have to change every place that initializes the variable when you don't need to specify a value.

Why super doesn't need to get allocated?

We always alloc before init in Objective C, then while writing init method, Why we don't alloc a super and then initiate it?
- (id) init {
if(self = [super init]){
//init iVars
}
return self;
}
When the child interface is allocated, its size includes the size of the parent. They aren't separate objects, they're combined. 'super' must be initialized so it can prepare its data members. Your object then initializes its members. For example, lets say you had a custom reference counting interface that needs to initialize 'refCnt' to 1. You then base your interface on that interface. That means your interface's allocation size is the size of the reference counting interface + the size of your specific contributions. (e.g., 'int refCnt' in the reference counting interface and 'int x' in your interface means sizeof(your interface)==8~.) So, the allocation only needs to occur once. Then, you initialize 'super' (the reference counting interface, in this situation) so 'refCnt' will be 1. After that, you initialize your own data.
Because "alloc" already allocates the space for the object you're instantiating.
Your subclass doesn't need to allocate (separate) space for a base class.
You also might find some useful information in this related question.

How are const variables created in Objective-C Classes?

Are they given assigned a value the moment they are declared (the interface) or are they assigned a value in the constructor of the class (the implementation)? If possible please give a brief example of how constant variables are assigned values in classes.
Objective-C does not support const instance variables. All instances variables are initialized to zero or nil when the class is instantiated.

Return an object as readonly

How would you return an object from a method, so it is read-only for the caller?
Please note that this isn't a property that can be simply set to read-only when it's getter is declared
i.e #property(nonatomic,retain,readonly) NSDate* pub_date;
For example:
-(SomeClass*)getObject
{
SomeClass* object = [[SomeClass alloc] init];
//Don't allow writing to 'object'
return object;
}
Thanks.
Short answer: there's no simple way of doing this.
Longer answer: Apple's framework defines a sort of standard for its collection classes where the immutable collection is the base class and the mutable collection is the inheriting class. So, for example, NSMutableArray inherits from NSArray. You can follow that standard, and have methods that return MyClass to clients while using MyMutableClass inside them. Technically the client can still send the mutating messages, of course, but IMHO that's not a big risk (after all, the client doesn't know your implementation details).
There are other, more complicated options - you can use pointer swizzling, or subclass and override all mutating methods, or simply copy the mutable class into an immutable counterpart (that's not complicated but may incur a performance hit). But for best results you should probably follow Apple's example.
It depends what the object is. If it has a mutable / immutable pair (like NSString/ NSMutableString) then your getter method can return the immutable version.
Otherwise, you can't control the behaviour of other objects - once you've returned an object, there is no control over it from the object that originally provided it.
If you are concerned that another object may alter an object returned from a getter, and thereby amend the property held within the original object, then you should return a copy of the object instead.
Example:
Object A has a mutable string property, object B asks for this mutable string, the getter directly returns the instance variable backing the property.
Object B then changes the string - the property of object A has also been amended because both objects have a pointer to the same mutable string.
In this case, you would return a copy of the object rather than the object itself. If your object is a custom one, you must implement the NSCopying protocol to allow this.
A further note - declaring a property as read only simply means that no setter accessor will be generated - i.e. objectA.property = newValue; will result in a compiler error.

What is the difference between "instantiated" and "initialized"?

I've been hearing these two words used in Microsoft tutorials for VB.NET. What is the difference between these two words when used in reference to variables?
Value vis-a-vis Reference Types
Variables in C# are in 1 of 2 groups. Value types or Reference types. Types like int and DateTime are value types. In contrast, any class you create is a reference type. C# strings are also a reference type. Most things in the .NET framework are reference types.
Parts of a Variable
There is the variable name and its value. Two parts.
The variable's name is what you declare it to be. The value is what you assign to it.
Variables are Initialized
All variables are always given an initial value at the point the variable is declared. Thus all variables are initialized.
For value types, like int the compiler will give them a valid value if you do not do so explicitly. int's initialize to zero by default, DateTime's initialize to DateTime.MinValue by default.
Reference type variables initialize to the object you give it. The compiler will not assign an object (i.e. a valid value) if you don't. In this case the value is null - nothing. So we say that the reference is initialized to null.
Objects are Instantiated
Humans are born. Objects are instantiated. A baby is an instance of a Human, an object is an instance of some Class.
The act of creating an instance of a Class is called instantiation (Ta-Da!)
So declare, initialize, and instantiate come together like this
MyClass myClassyReference = new MyClass();
In the above, it is wrong to say "... creating an instance of an object..."
edit - inspired by comments discussion
Three distinct things are going on (above) using distinct terminology and that terminology is not interchangeable :
A reference variable is declared - MyClass myClassyReference
An object is instantiated (...from/of a given class, implied) - new MyClass()
The object is assigned to the variable. =.
Restating the facts:
A reference-type variable is also called simply "a reference". A "value-type variable" is not a reference.
This: "objectA is an instance of an object" is profoundly wrong. If objectA was "an instance of objectB" then it must be that objectA begins life with objectB's type - whatever that is - and current state - whatever that is. What about creating objects D, E, and F as objectB changes? Nay, nay! It is the conceptual and technical case the "objectA is an instance of a Class". "Instantiation" and "instance of" have precise meaning - an object gets its type, definitions, and values from a Class.
MyClass myClassyReference = null Generally we don't say "the variable is assigned to null" and we never say "the variable is referencing null", No. instead we say "the variable is null"; or "the variable is not referencing anything", or "the reference is null"
Practical Application:
I jab my finger at your code and say "this instance has an invalid property. Maybe that's why the loop fails. You gotta validate parameters during instantiation." (i.e. constructor arguments).
I see this in your code ,
MyClass myClassyReference;
myClassyReference.DoSomething();
"You declared the variable but never assigned it. it's null so it's not referencing anything. That's why the method call throws an exception."
end edit
The Unbearable Lightness of Being
A reference type variable's name and value exists independently. And I do mean independent.
An instantiated object may or may not have a reference to it.
An instantiated object may have many references to it.
A declared reference may or may not be pointing to an object.
A variable is initialized with a value. An object is instantiated when memory is allocated for it and it's constructor has been run.
For instance here is a variable:
Dim obj as Object
This variable has not been initialized. Once I assign a value to the obj variable, the variable will be initialized. Here are examples of initialization:
obj = 1
obj = "foo"
Instantiation is a very different thing but is related since instantiation is usually followed by initialization:
Dim obj As New Object()
In the preceding line of code, the obj variable is initialized with the reference to the new Object that was instantiated. We say that the new Object was instantiated because we have created a new instance of it.
Now I believe that VB.NET makes this a lot more confusing than C# because it is not clear that an assignment is taking place in the code above. In C# it is much clearer that there is both an instantiation of an instance and an initialization of a variable:
Object obj = new Object();
To initialize something is to set it to its initial value. To instantiate something is to create an instance of it.
Often this is the more or less same thing. This:
SqlConnection conn = new SqlConnection();
instantiates a SqlConnection object, and initializes the conn variable by setting it to the that instance.
Since an object's constructor also sets the object's properties to their default values, it's often correct to say that instantiating an object initializes it. (Misleading, if the object exposes a method that you have to explictly call to initialize it after it's instantiated, as is sometimes the case.)
*Instantiation means to create an instance for a class or object.Initialization means to *initiate the same object or class for any purpose.**
Instantiated means that an instance of the object has been created. Initiated means that that same object has done some initialization.
When you instantiate a class or object, you're creating a new instance of it, or allocating memory to "hold" one. Initializing that object would be the instructions that are performed during instantiation.
Instantiation is when you create an instance of a class. That instance is then an object, and you can set its properties, or call methods on it (tell it to do things).
Initiation is when you set up a set of initial conditions for something. That something might be an object, where you tell it to initiate itself, or just a variable to which you assign a value.
An object might initialise some other things, or even instantiate other objects as part of its initiation.
The difference is that instantiation is creation of a thing that can do stuff; initiation is stuff that gets done.
See the Java docs:
https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
"Point originOne = new Point(23, 94);
Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.
Instantiation: The new keyword is a Java operator that creates the object.
Initialization: The new operator is followed by a call to a constructor, which initializes the new object."
We can see it this way. For a line of code below:
var p = new Person();
The above line can be read as following two ways:
The variable p has been initialized as a person class
Person class has been instantiated in variable p
The subject of reference or context matters. When talking in terms of variable, we use the word initialize. When talking in terms of class/type, we use the word instantiate.
Instantiation refers to the allocation of memory to create an instance of a class whereas initialization refers to naming that instance by assigning the variable name to that instance.
Eg: SqlConnection conn = new SqlConnection();
Here new is a keyword which allocates memory for an instance and conn is a variable name assigned for that instance.
Others have explained the difference, so I wont go into detail. But there are cases where instantiation does not properly initialize an object. When you instantiate an object you also initialize it with some data. The class/type will have the initialization logic, whereas the instantiation logic is typically carried out by thenew keyword (basically memory allocation, reference copying etc). But instantiation need not necessarily result in a valid state for objects which is when we can say the object is uninitialzed. Here's a practical example where an object can be instantiated but not initialized (sorry e.g. in C#).
class P { string name = "Ralf"; }
WriteLine(new P().name); // "Ralf";
WriteLine((FormatterServices.GetUninitializedObject(typeof(P)) as P).name); // null
GetUninitializedObject doesn't call the constructor to instantiate object there (but some internal magic).
One could also argue value types are not instantiated but only initialized as it doesn't need new allocation when you do new.. but that's up to one's definition of instantiation.
In object-oriented parlance:
To instantiate means creating an object of some class, which initial state may be undefined.
The class is a blueprint which is used by the program to create objects. Objects created are compliant with the blueprint and can be manipulated by the program. E.g. variables current_client and previous_client can be assigned objects of class Customer. An instance of class X is an object instantiated from class X.
In the code the class is a permanent static description of what an object can do, but the objects themselves are temporary and dynamic. They have an individual state which can be changed (e.g. the Customer name, the associated orders). Instantiation can be done like this:
dim current_client as new Customer (VB)
Customer* current_client = new Customer() (C++)
current_client = Customer() (Python)
new Customer, new Customer() and Customer() are equivalent forms in different languages to trigger the instantiation.
In the end objects are destructed to release memory and other resources required for their existence and working.
To initialize means assigning an initial state to the object before it is used.
This initialization can be part of the instantiation process, in that case values are explicitly assigned to object attributes in the constructor of the object. Alternatively it can be left to the user who can decide whether it is required or not. The latter method allows faster instantiation, but requires the user's code to not read the value of any attribute before this code has explicitly assigned a value to this attribute. E.g. this code:
current_client.count = current_client.count + 1
is not allowed before the attribute count has been set by the user, since it can contain any initial value, including an invalid value which would trigger an execution error.