-1

I know Singleton class is a class whose only one object can be created at a time.

My questions are:

1.What is the use of Singleton class in objective-c?

2.How to create and use the created Singleton class?

Rui Peres
  • 25,462
  • 9
  • 86
  • 133
liza
  • 3,667
  • 2
  • 15
  • 18
  • I didnt get any satisfactory answer, so i asked here – liza Apr 25 '13 at 07:25
  • 1.) the same as in any other language 2.) http://stackoverflow.com/questions/5381085/how-to-create-singleton-class-in-objective-c?lq=1 – peko Apr 25 '13 at 08:16
  • Singleton Class have only single instance in a memory at time so what ever object we crated and destroy it but normal class declaration we crated many objects so some times you don't destroy it right time so it will produce memory leaks. – annu May 31 '17 at 14:26

3 Answers3

12

You normally use a Singleton when you want to expose something to the entire project, or you want a single point of entry to something. Imagine that you have a photos application, and 3 our 4 UIViewControllers need to access an Array with Photos. It might (most of the time it doesn't) make sense to have a Singleton to have a reference to those photos.

A quick implementation can be found here. And would look like this:

+ (id)sharedManager
{
  static id sharedManager;
  static dispatch_once_t once;
  dispatch_once(&once, ^{
    sharedManager = [[self alloc] init];
  });
  return sharedManager;
}

You can see other ways of implementing this pattern here.

In Swift 2.1 would look like this:

class Manager {

    static let sharedManager = Manager()
    private init() { }
}
Rui Peres
  • 25,462
  • 9
  • 86
  • 133
  • Singleton Class have only single instance in a memory at time so what ever object we crated and destroy it but normal class declaration we crated many objects so some times you don't destroy it right time so it will produce memory leaks. – annu May 31 '17 at 14:26
7

A singleton is a special kind of class where only one instance of the class exists for the current process. In the case of an iPhone app, the one instance is shared across the entire app.

Have a look at these tutorials :

http://pixeleap.com/?p=19

http://www.codeproject.com/Tips/232321/Implement-Objective-C-Singleton-Pattern

http://xcodenoobies.blogspot.in/2012/08/how-to-pass-data-between.html

http://www.johnwordsworth.com/2010/04/iphone-code-snippet-the-singleton-pattern/

http://www.idev101.com/code/Objective-C/singletons.html

And this video tutorial :

http://www.youtube.com/watch?v=FTfEN8KQPK8

Vineet Singh
  • 4,793
  • 1
  • 29
  • 39
  • Singleton Class have only single instance in a memory at time so what ever object we crated and destroy it but normal class declaration we crated many objects so some times you don't destroy it right time so it will produce memory leaks. – annu May 31 '17 at 14:25
3

Here is an example and tutorial: http://www.galloway.me.uk/tutorials/singleton-classes/

Here is another one: How to create singleton class in objective C

Singleton contains global variables and global functions.It’s an extremely powerful way to share data between different parts of code without having to pass the data around manually.

Community
  • 1
  • 1
lakshmen
  • 27,102
  • 64
  • 169
  • 262