Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Singleton class

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 418
    Comment on it

    A special type of class where only one instance per process is created and used is Singleton class. Singleton classes are commonly used the time when general services were offered by some classes. In Singleton class we can store values and wherever we want to access those values we can just use its sharedInstance which prevents from allocating memory every time.
    It provides a global point for accessing the resources of its class. Thus, Singleton class are useful in situations where single point of control is required for different classes.

    Here is an example which shows the UserName and UserPassword is being saved in the Singleton class and we can access it by using its sharedInstance :-

    Create a Singleton class which is a subclass of NSObject. In your singleton class .h file include the following code ..

    @interface DemoSingleton : NSObject
    
    +(DemoSingleton *)objectOfSingleton;     // class method to return the singleton object
    
    /* declare the property for the values which you want to save in singleton class */
    
    @property(nonatomic,retain)NSString *name;            
    @property(nonatomic,retain)NSString *passwordOfUser;
    
    @end
    

    Include this code in your singleton class .m file :-

    #import "DemoSingleton.h"
    
    @implementation DemoSingleton
    
    +(DemoSingleton* )objectOfSingleton{    
        
        static DemoSingleton *objectOfSIngleton=nil;
        static dispatch_once_t  oncePredecate;  
        dispatch_once(&oncePredecate,^{
           objectOfSIngleton = [[DemoSingleton alloc] init];  // memory allocated to the object of singleton
            
        });
        return objectOfSIngleton;
        
    }
    @end
    

     

    Now we have to access the properties of Singleton class so #include the header of Singleton in your class.. We can set the value or save the value in singleton class as :

       DemoSingleton *object=[DemoSingleton objectOfSingleton];
    
       object.name = self.txtName.text;    // self.txtName.text is the outlet of textField in which we enter the name to save
    
       object.passwordOfUser = self.txtPassword.text;     //self.txtPassword.text is the outlet of textField in which we enter the password to save

    The values are saved and wherever we want to use it we can just access the properties of singleton class using its object as :-

    DemoSingleton *obj=[DemoSingleton objectOfSingleton];
    
    self.lblNameOfUser.text = obj.name;              //self.lblNameOfUser.text is the label to display name
    
    self.lblPasswordlOfUser.text = obj.eMailOfUser;     //self.lblPasswordlOfUser.text is the label to display password
    

    It's done !

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: