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

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.

Related

Swift how to load a class [duplicate]

This question already has answers here:
Method load() defines Objective-C class method 'load', which is not permitted by Swift 1.2
(5 answers)
Closed 6 years ago.
As we knew, when an Objective-C class was loaded the +load method would be called. But in Swift we don't have +load method.
So I tried to invested this. I added a symbol breakpoint at call_load_methods. In Objective-C world. this function will call all the class's +load. But it didn't enter this breakpoint.
So I wondering how to load in Swift. Or can you guys give me more detail a about Swift runtime or something else?
Unfortunately, a load class method implemented in Swift is never
called by the runtime, rendering that recommendation an impossibility.
Instead, we're left to pick among second-choice options:
Implement method swizzling in initialize. This can be done safely, so
long as you check the type at execution time and wrap the swizzling in
dispatch_once (which you should be doing anyway). Implement method
swizzling in the app delegate. Instead of adding method swizzling via
a class extension, simply add a method to the app delegate to be
executed when application(_:didFinishLaunchingWithOptions:) is called.
Depending on the classes you're modifying, this may be sufficient and
should guarantee your code is executed every time.
source

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

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

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?