IOS Development Learning SUMMARY objective-c object-oriented-member variables, simulation variables, Singleton Mode

Source: Internet
Author: User

IOS Development Learning SUMMARY objective-c object-oriented-member variables, simulation variables, Singleton Mode

Variables in OC can be divided into three categories based on their defined locations: member variables, local variables, and global variables.
As mentioned before, methods are evolved from functions. Therefore, both variables defined in methods and variables defined in functions are local variables.

Member variables and their running mechanism

Member variables:It refers to the variables defined in the class interface or class implementation.
Note: objective-c's member variables are instance variables and do not support true class variables.
The instance variable exists from the creation of this class until the system completely destroys this instance. The instance variable scope is the same as that of the corresponding instance. Instance variables can be understood as instance member variables, which act as a member of an instance and coexist with the instance.
As long as the instance exists, the program can access the instance variables of the instance. The syntax for accessing instance variables in the program is as follows:Instance-> instance variable;
Example program:

# Import
  
   
@ Interface FKPerson: NSObject {@ public // defines two instance variables NSString * _ name; int _ age;} @ end @ implementation FKPerson @ endint main (int argc, char * argv []) {@ autoreleasepool {// create the Person object FKPerson * p = [[FKPerson alloc] init]; // access the NSLog (value of the _ name and _ age instance variable of the Person object through the pointer variable) (the value of the _ name instance variable of the @ p variable is: % @, the value of the _ age member variable of the p object is % d, p-> _ name, p-> _ age ); // directly assign the value of p to the name instance variable p-> _ name = @ Sun Wukong; // directly assign the value of p to the age instance variable p-> _ age = 500; // access the NSLog (value of the _ name and _ age instance variable of the Person object through the pointer variable) again (the value of the _ name instance variable of the @ p variable is: % @, the value of the _ age member variable of the p object is % d, p-> _ name, p-> _ age ); // create the first FKPerson object FKPerson * p1 = [[FKPerson alloc] init]; // create the second FKPerson object FKPerson * p2 = [[FKPerson alloc] init]; // assign values to the name member variables of the two FKPerson objects respectively: p1-> _ name = @ Zhang San; p2-> _ name = @ Sun Wukong ;}}
  

Summary:The member variables do not need to be explicitly initialized. If an instance variable is defined for a class, the system will perform the default initialization for the instance variable. Instance variables of the basic type are initialized to 0 by default, and member variables of the pointer type are initialized to nil by default.

From the perspective of memory storage, objective-c objects are similar to C struct objects.

Use the class defined in the above code to create two instances. It describes the initialization of instance variables in OC and the running mechanism in memory.

// Create the first FKPerson object FKPerson * p1 = [[FKPerson alloc] init];
// Create the second FKPerson object FKPerson * p2 = [[FKPerson alloc] init];
// Assign values to the name member variables of the two FKPerson objects respectively: p1-> _ name = @ Zhang San; p2-> _ name = @ Sun Wukong;



Simulation Variables

Simulate class variables by using internal local variables.
** Note: ** the static keyword cannot modify member variables, but can only modify local variables, global variables, and functions.
To simulate a class variable, you can define a static global variable in the class implementation section and provide a class method to expose the global variable.
Sample Code:
Header file: FKUser. h

#import 
  
   @interface FKUser : NSObject+ (NSString*) nation;+ (void) setNation: (NSString*) newNation;@end
  

Implementation file: FKUser. m

# Import FKUser. hstatic NSString * nation = nil; @ implementation FKUser + (NSString *) nation {// return the nation global variable return nation;} + (void) setNation: (NSString *) newNation {// assign a value to the nation global variable if (! [Nation isEqualToString: newNation]) {nation = newNation ;}@endint main (int argc, char * argv []) {@ autoreleasepool {// assign a value to the FKUser class variable [FKUser setNation: @ China]; // access the FKUser class variable NSLog (@ FKUser's nation class variable is: % @, [FKUser nation]) ;}}
Singleton Mode

If a class can only create one instance at a time, this class is calledSingleton class.
The singleton class can be implemented through static global variables. The program considers defining a static global variable to save the created singleton object. Each time the program needs to obtain the instance, first, determine whether the static global variable is nil. If it is nil, initialize an instance and assign a value to the static global variable.

Example program:
FKSingleton. h

#import 
  
   @interface FKSingleton : NSObject+ (id) instance;@end
  

FKSingleton. m

# Import FKSingleton. hstatic id instance = nil; @ implementation FKSingleton + (id) instance {// if the instance is nil if (! Instance) {// create a Singleton instance and assign it to the instance global variable instance = [[super alloc] init];} return instance ;} @ endint main (int argc, char * argv []) {@ autoreleasepool {// check whether the two retrieved instances are equal. The program returns 1 (true) NSLog (@ % d, [FKSingleton instance] = [FKSingleton instance]);}

 

Related Article

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.