Singleton, Method & Variable - objective-c

i would like just to get answer to this question, i know it is possible with variables, but with method ?
with a singleton, can i use methods + variables anywhere in my project ?
Thanks.

A singleton is nothing but a global instance of a class. Once you get the instance anywhere on your project, you're supposed to do whatever you want with it, as you would with any other class instance. Obviously, you can access it's public variables and methods.

Yes, you won't have to create an instance to access your singleton method.

Related

Why singleton object is made through class methods?

We always make singleton object through Class method.What will happened if i make that method instance(use - instead of +) and call that method through a nil object or simply through an object?
Conceptually a the instance method is not attached to any specific instance of a class.
This means that from a design point of view it doesn't make sense to invoke it on an existing object. It's like requiring that a +(int) sum:(int)a with:(int)b should be an instance method when it doesn't need to know anything about an object.
In addition usually a singleton class constructor shouldn't be accessible from clients, if you need to instantiate one to call -instance on it this would defeat the purpose.
Last thing, a singleton should be the only possible instance of an object, if you need to have one to instantiate one then you are in a deadlock: you need an instance to create an instance but you are no allowed to have more than an instance.
Regarding calling it on nil object, if I remember correctly a pointer returned from a message sent to nil will always be nil too.

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.

Application-wide variable access: static like Java?

I have an instance variable in my view controller that I would like to share with the whole program. I'm not quite sure how to do this. Can I just declare it as a static instance variable and then access it through properties like ViewControllerClass.instancevariable?
Thanks!
In reply to your comment attached to the question:
If you have an instance variable in Object X, any other object that has a reference to X can access that variable through the usual means:
[objectX myClassAInstanceVariable];
// Or, if declared with #property
objectX.myClassAInstanceVariable;
If the other object doesn't have a reference to X, then it can't access the variable, no matter the state or kind of the variable. In this case, you may want to rethink your design, or see my last paragraph below about the app delegate.
The concept of "static" is different in Objective-C than what you may be expecting (it sounds like you're coming from experience with Java).
Objective-C doesn't really have a concept of "class variables", although it is fairly easy to simulate such a thing; this is described in the question that mathk linked to: Objective-C Static Class Level variables. You declare a variable in the class's header file which is static, so that it is inaccessible outside that file, and create accessor class methods for it.
Any object that has a reference to your view controller can send messages to it. Note that in Objective-C, member variables are "protected" by default, meaning that only an instance of a class or subclasses, not other objects, can access those variables. Other objects must go through setter and getter methods.
Just as another option, because the background of your question is not clear, if you have some kind of a "global variable" which isn't really specific to your view controller, a better place to put it may be the application delegate*. Any object can get a reference to the delegate at any time:
[NSApp delegate];
NSApp is a global reference to the NSApplication object which is at the core of your program.
*Although it's certainly possible to overdo this.

Objective-C: Class vs Instance Methods?

In Objective-C, while creating any class, how do we decide whether we need to mark a method as Class method or Instance Method ?
I know the difference between the 2, but my question is how to decide the marking (+/-) for any method ?
+ denotes a class method, - denotes an instance method. You create class or instance methods where your application needs them. Should you actually know the difference between the two, and your application, then you should have no problems understanding when to use which.
I believe you don't know the differences in how they apply to your application, so here's a small primer:
You use a class method when you need to access some behaviour globally through all instances of that class. i.e., [[self class] someSpecialThing];
You also use a class method when you need a factory method; and
Everywhere else, you use an instance method.

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