Objective-C Knowledge Summary (2), objective-c Knowledge Summary

Source: Internet
Author: User

Objective-C Knowledge Summary (2), objective-c Knowledge Summary

Define two methods for each instance variable: Set the setter method of the variable to get the getter method of the variable value

Set Method Writing specifications

Get method writing Specification

The following code demonstrates the set and get methods.

The code for creating a Person class in the. h file is as follows:

# Import <Foundation/Foundation. h> @ interface Person: NSObject {NSString * _ name; int _ age;} // Declaration of the setter method and getter method of the member Variable _ name-(void) setName :( NSString *) name;-(NSString *) name; // Declaration of the setter and getter methods of member Variable _ age-(void) setAge :( int) age; -(int) age; @ end

The code in the. m file is as follows:

# Import "Person. h "@ implementation Person // setter method of member Variable _ name AND implementation of the getter method-(void) setName :( NSString *) name {_ name = name ;} -(NSString *) name {return _ name;} // setter method and getter method of member Variable _ age-(void) setAge :( int) age {// specify whether the input age is valid. if (age> = 18) {_ age = age;} else {_ age = 18 ;}}-(int) age {return _ age ;}@ end

There is no method overload in OC. One of the so-called parameters is because the method name has changed and the prompt is: do ::::

Use of the static keyword in OC: The only thing to note is that the member variable cannot be defined as static. In other cases, it is used in the same way as in C, please refer to my previous blog> --- Click here to see --- <

The self keyword in OC. self uses class methods to represent this class. self uses Object methods to represent this object.

Self modifier variable: access the member variable. Please refer to the Code: (still according to the Person class) in the. m file.

// Setter method of member Variable _ name and implementation of getter method-(void) setName :( NSString *) name {self-> speed = speed; // self-> speed is equivalent to _ name/_ name = name;}-(NSString *) name {return _ name ;}

Here, let's talk about the four main features of objects:

1. abstraction 2. encapsulation 3. Inheritance 4. Polymorphism

Abstract: Abstraction ignores the aspects irrelevant to the current target in a topic so that you can pay more attention to the aspects related to the current target. Abstraction is not intended to understand all the problems, but to select a part of the problem. For example, we want to design a student achievement management system. When we look at the student, we only care about his class, student ID, and score, instead of caring about his height and weight. Abstract: Process abstraction and data abstraction. Process abstraction means that any operation with clearly defined functions can be viewed by users as a single entity, although this operation may actually be completed by a series of lower-level operations. Data abstraction defines the data type and operations applied to this type of object, and limits the object values to be modified and observed only by using these operations.

For information about encapsulation, inheritance, and polymorphism, please refer to my previous blog (also introduced classification)> --- click here --- <

Introduction to member variable Modifiers

  • @ Public, accessible anywhere through instance objects
  • @ Private, which indicates that it can only be used in the current class and cannot be used in subclass, but inherited by the quilt class

  • @ Protected: the protected type, indicating that it can only be accessed in the current class and subclass (protected by default)

Description Method: override the description method of the parent class to change the output of NSLog.

If NSLog is used to print self in the-description method, an endless loop is generated.

The Code is as follows:

// Rewrite the description-(NSString *) description of the parent class {// do something .... // For example, return [NSString stringWithFormat: <# (NSString *),... #>];}

The essence of a class is an object.

Class Object belongs to the Class type

// Two methods for obtaining Class objects: class c = [Person Class]; // class method // or Person * p = [Person new]; class c2 = [p class]; // object Method

Member variables cannot be called in class methods.

SEL

1. storage location of the Method

  • The method list of each class is stored in the class object.
  • Each method has a SEL-type object.
  • You can find the method address based on a SEL object and call the method.
  • Definition of SEL type
typedef struct objc_selector     *SEL;

2. Create a SEL object

SEL s = @selector(test);SEL s2 = NSSelectorFromString(@"test");

3. Other SEL object usage

// Convert SEL object to NSString object NSString * str = NSStringFromSelector (@ selector (test); Person * p = [Person new]; // call the test method of object p [p performSelector: @ selector (test)];

The dot syntax in OC is a feature of Xcode. Xcode helps us replace the code.

// Xcode will replace the following code with [person setAge: 18]; person. age = 18; // Xcode will replace the following code with [person age]; int a = person. age;

@ Property and @ synthesize

@ Property: the compiler instruction. The compiler will help us declare the _ age _ name get/set method.

@ Synthesize helps us implement the get and set methods of instance variables

After Xcode4.4 @ property can help me declare the _ age _ name get/set method, or implement the get and set methods of instance variables.

@property (nonatomic, assign) int age;@property (nonatomic, copy) NSString *name;

Id is a universal pointer that can point to any object

Constructor in OC

Override the constructor to make the object have an initial value after it is successfully created.

The initialization method for the object in OC is: init object method this method returns an object (the object that calls the init method)

// When the subclass overwrites the init of the parent class, this is the default instancetype of the first subclass) init {// Let the parent class finish the original tasks of the parent class self = [super init]; // determine whether the parent class is initialized successfully if (self) {// write the content initialized by the subclass here _ age = 18; // set the age to the default value of 18 years} return self; // The caller of the method referenced by self}

Custom Constructor

The. h file code is as follows:

# Import <Foundation/Foundation. h> @ interface Person: NSObject @ property (nonatomic, copy) NSString * name; @ property (nonatomic, assign) int age; // custom constructor-(instancetype) initWithName :( NSString *) name andAge :( int) age; @ end

The. m file code is as follows:

#import "Person.h"@implementation Person- (instancetype)initWithName:(NSString *)name andAge:(int)age{    if (self = [super init]) {        _name = name;        _age = age;    }    return self;}@end

 

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.