Use of "Self" keyword in Objective-C [duplicate] - objective-c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Objective-C - When to use ‘self’
I can't understand very well the importance and the usage of the "self" keyword in objective-c. It's my first OOP language so i got stuck on some concepts.
When i should use "self"? Why is it useful?
Thanks for the answers!
Edit: I can't understand why is this a duplicated post of Objective-C - When to use 'self' when there there is not the explanation of "Self" that i wanted.

self is a special variable in Objective-C, inside an instance method this variable refers to the receiver(object) of the message that invoked the method, while in a class method self will indicate which class is calling.
self refers to the actual object that is executing the current method, it is an invisible argument passed automatically by the runtime environment to your instance methods.

Related

Is a "property" some kind of "instance variable" in Objective-C or are they different? [duplicate]

This question already has answers here:
Is there a difference between an "instance variable" and a "property" in Objective-c?
(6 answers)
Closed 7 years ago.
Is there much of a difference between "property" and an "instance variable" in Objective-C?
I have been led to believe that an instance variable that has accessor methods is knows as a "property", but I now I think that this might not be true.
An instance variable is unique to a class. By default, only the class and subclasses can access it. Therefore, as a fundamental principal of object-oriented programming, instance variables (ivars) are private—they are encapsulated by the class.
By contrast, a property is a public value that may or may not correspond to an instance variable. If you want to make an ivar public, you'd probably make a corresponding property.
There is a nice blog to go through. Also, go through the link shared by katleta300 above.

Allow only a particular class to call init in iOS [duplicate]

This question already has answers here:
Singleton pattern in objc, how to keep init private?
(4 answers)
Closed 7 years ago.
I was trying something new so just going through a thought process to allow only my singleton class to create new instance for other classes which are hidden behind it.
I am able to restrict to call the init method using the "unavailable" attribute along with my init method but can I create an exception that only my Singleton class can call the init method.
but can I create an exception that only my Singleton class can call the init method
You can't know who the caller is — see this discussion of the matter — but you can certainly hide the init method by not putting it in your header. A commonly used solution is, if possible, to have the singleton factory method call a "private" initializer and throw an exception on the "public" initializer. Of course that doesn't ultimately prevent anyone from calling the "private" initializer - sorry, Objective-C is too dynamic for that - but it makes it harder to obtain a non-singleton instance by mistake.

Call Objective-C object method from C function [duplicate]

This question already has answers here:
How to call an Objective-C Method from a C Method?
(5 answers)
Closed 8 years ago.
I do have an objective-c object myObject.
Inside this object I do have some C-functions.
static void C_doThing(void) {
...
}
Inside myObject i can just call C_doThing() and the function gets called.
Now I want to call a method of myObject from this C-function.
Is there a way to do this? "Self" is not available inside the C-function.
Thanks
No; the C function is just a plain C function that happens to be defined inside your .m file, but it's not actually "part" of the object (class). It just happens to be stuck sitting there in the same file.
You could pass the object (self in the caller) in as an argument to the function, and then call methods on it.
But you should probably just make it into a normal instance method. You've defined it as static, which means it has no visibility outside of the file. If that's the case, then why not make it a method?
(There may be a small number of good cases for using utility C functions that are used only within a single class implementation, but they're pretty rare and typically around serious performance optimization.)

What is the (id) mean in the init method? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the meaning of id?
I am the newbie to Ios programming.
I saw the following declaration
- (id)init
what does (id) mean here?
id denotes a type which is compatible with any object. The notation
- (id)init
means the init instance method of your class; typically it's used to initialize the instantiated object after memory allocation (usually done using alloc). In Objective-C, methods' return type is declared by putting the type in parentheses before the method name. So, here it means that the init method may return any Objective-C object.
But you should really, really google an Objetive-C tutorial and read it. This is such a fundamental thing for which there is no excuse for not reading a tutorial or other documentation.
id is the plain C compatible type that represents an Objective-C object. This allows C source code to store, and interact with, Objective-C objects.
The reason for it to be of type 'id' is that the -init method is inherited all the way up from NSObject (in objective-C you can not overload methods, hence you can not change the argument/retrurn value types when subclassing). Since 'id' works with any object, this is OK.
EDIT It seems that specifying a concrete class as the return type of -init is OK, even though you are ultimately overriding '-[NSObject init]'.
I guess the use of 'id' is just a custom?
The fact that 'id' acts as a "generic Objective-C object pointer" that accepts any object type on assignment remains unchanged, though.
-(id)init is called to initialize the variables inside an object and do any necessary setup (it's basically the constructor).
A possiable duplication can be
What's the -(id)init method good for?

Use-case of +new in Cocoa? And why it exist? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
alloc, init, and new in Objective-C
There is +alloc/-init... and +new. Their role are almost same in documentation except +new is designed for instance pooling. (according to my understanding :)
However the pooling is possible with +alloc/-init. Why the separated method is required? Or is there any reason for the method?
And I couldn't find any example utilizes the method. When should I use this method? Can I get some use-case of the method +new?
In the original Objective-C implementation from Stepstone, +new was what we used to create instances. It was a holdover from Smalltalk. NeXT separated +new into +alloc/-init since most classes don't have to do anything different to allocate the memory their instances use, and it didn't make sense to duplicate that code all over the place.