Single-pass value of iOS value

Source: Internet
Author: User

In iOS apps, there are often values and variable sharing in different view and applications, and there are several ways to do this:

1.extern mode

2. Single-case mode

3.delegate mode


The singleton pattern, as the name implies, is only one instance, which ensures that a class has only one instance, and instantiates it itself and provides it to the system as a whole. It is often used to do application-level shared resource control. This mode is used very frequently, and through a singleton class, you can implement parameter passing between different view


#import <Foundation/Foundation.h> @interface session:nsobject@property (strong,nonatomic) NSString * singlevalue;//implementation of a single case method + (Session *) getinstance; @end


#import "Session.h" @implementation session//single-instance static Session *instance;//Singleton + (Session *) getinstance {    @ Synchronized (self) {        if (instance = = Nil) {            instance = [[Self alloc] init];                    }    }    return instance;} -(ID) init{    if (self = [super init]) {        self.singlevalue = [[NSString alloc] init];    }    return self;} @end


Then, in the case where you need to use a singleton class import, this singleton class

Session *session = [session getinstance];

Session.singlevalue = @ "Good magic!" ";

NSString *value = Session.singlevalue;


Single-pass value of iOS value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.