1. Definitions of the settings and accessors
* The method of assigning a value to a single instance variable is called a set. (Setter method)
* The method to give a single instance variable value is called an accessor. (Getter method)
2. Why use the setter and accessor (getter)
* The visibility of instance variables is:
1). Private: Accessed only in this type of file, i.e.. h and. m files, access by: direct access.
2). Protected: Accessed in this class of files and subclass files. Access: Direct access.
3). Public: This type of file and subclass files and other files can be accessed. Access: In this class and subclass is direct access, in other Files: Object (instance access)
* The system default instance variable visibility is @protected.
* 1. Why not default @public?
* @public or cause instance variables to be exposed, accessible anywhere, unsafe, contrary to the idea of encapsulation (convenient for others, embarrassing yourself).
* 2. Why not default @private?
* @private causes instances of the parent class to become non-accessible, violating the idea of inheritance (subclass inheritance and the ability to access content inherited from the parent class).
3.settter and Getter Naming conventions
* Setter Method Naming specification * : Minus method with no return value, starting with SET + instance variable name capitalized, with a parameter with the same type as the instance variable, parameter name and instance variable name. * Getter Method naming specification : The minus method has no return value. The return value type is the same as the instance variable type, with the same method name and instance variable name without parameters. *
Example: Person.h file
@interface person:nsobject{ nsstring *_name; NSString *_sex;} Setter and getter-(void) Setsex: (NSString *) sex;-(NSString *) sex;-(void) SetName: (NSString *) name;-(NSString *) name;@ End
2). Implement getter and Setter methods: PERSON.M file,
@implementation person-(void) SetName: (NSString *) name { _name = name;} -(NSString *) name { return _name;} -(void) Setsex: (NSString *) Sex { _sex = sex;} -(NSString *) Sex { return _sex;} @end
3). Accessing instance variables
{person *person = [[Person alloc] init]; [Person setname:@ "ASD"]; NSString *name1 = [person name]; NSLog (@ "%@", name1); }
4. Custom initialization of Objects
Custom initialization: If you want to assign a meaningful initial value to the object initialization, you need to customize the initialization method
Example: 1). declaration
Give the person object, initialize assignment-(ID) Initwithname: (NSString *) name sex: (NSString *) sex;
2). Custom initialization method implementation.
The initialization operation is also an assignment operation, except that the process is in the initialization phase.-(ID) Initwithname: (NSString *) name sex: (NSString *) Sex {self = [super init]; if (self! = nil) { _name = name; _sex = sex; } Return self;//represents the object that called the method in the minus method.
5. Multi-parameter assignment.
Example: 1) statement
-(void) SetName: (NSString *) name sex: (NSString *) sex;
2). Implement
Multi-parameter assignment implementation-(void) SetName: (NSString *) name sex: (NSString *) Sex { _name = name; _sex = sex;}
6. Custom initialization and multipledifferences in Parameter assignments
1. Timing is different: The initialization method is in the initialization phase, the assignment method is after the object is created
2. The number of calls is different: Initialize a method can only be called once in the life cycle, the assignment method calls multiple times.
3. Different functions: Initialization is assignment, and assignment is a change value.
Initialization
An assignment is made immediately after the object has been opened up, and this process is called initialization, and the space for an object is opened only once, and all initialization processes are only once.
Note:#pragma mark-plus method//(Compile indicator: pragma) when the function method is too large, you can clearly query the plus minus method
"Learning iOS Path: Object-c", accessors, custom initialization and multi-parameter assignment