Objective-C: the fourth day of grammar learning

Source: Internet
Author: User

Objective-C: the fourth day of grammar learning

About NSObject and runtime system

Class NSObject

As a dynamic programming language, OC has many dynamic features. It not only requires the compiling environment, but also a runtime system to execute compiled code. The role played by the runtime system is similar to the OC operating system. It is responsible for independently generating objects and managing memory when released.

The functions provided by the runtime system cannot be directly used in the program. The root class method provides the basic functions of the runtime system and inherits all NSObject classes to freely use the runtime functions. That is to say, the root class is equivalent to an excuse for the runtime system.

1. Classes and Instances

NSObejct has only one instance variable, that is, the Class type variable isa. Isa identifies the Class Object of the Instance Object. Isa identifies the Class Object of the Instance Object. Because isa determines the relationship between instance variables and classes and is very important, the isa value cannot be modified for subclasses. You cannot directly access isa to query which class the variable belongs. Instead, you must use the instance method class to complete the query.

The NSObject method is defined for its subclass and all instance objects rather than for itself.

-(Calss) class; return the class Object of the class to which the receiver belongs.

+ (Class) class; return Class Object

-(Id) self; returns the recipient itself. Is a method that does not have any actual action, but is very good.

-(Bool) isMemberOfClass :( Class) aClass; determines whether the message receiver is an object of the aClass Class parameter.

-(Bool) isKindOfClass :( Class) aClass determines whether the message receiver is an instance of the aClass operator or a subclass of the aClass Class. This is different from isMemberOfClass because YES is returned when the message receiver is an instance of the aClass subclass;

-(Bool) isSubclassOfClass :( Class) aClass; determines whether the message receiver is a subclass or itself of the aClass parameter. If so, YES is returned.

-(Class) superclass; returns the Class Object of the parent Class of the message recipient;

+ (Class) superclass; return the Class Object of the parent Class of the message receiving Class

2. Generation and release of instance objects

+ (Id) alloc;

-(Void) dealloc; release the Instance Object

-(Oneway void) release;

-(Id) retain;

-(Id) autorelease;

-(NSUInterger) retainCount;

-(Void) finalize; the Garbage Collector executes the finalize method before releasing the recipient object.

The methods from congdealloc to retainCount are called when the count is manually referenced to manage the memory. They are unavailable when the ARC is used, and finalize is only used when garbage collection is effective.

3. Initialization

-(Id) init

-(Void) initialize is used for class initialization, that is, to initialize the variables used in the class. This method is automatically called before the class receives the first message, it cannot be called manually and will only be called once.

+ (Id) new; new is a combination of alloc and init.

4. Comparison of Objects

-(Bool) isEqual :( id) anObject; if the message receiver is equal to the anObject parameter, YES is returned.

-(NSUinteger) hash. when an object is put into a container, the system returns the hash value used internally. [Two objects with the same hash value may not be equal. The quick search system generally has a hash list, and the Foundation framework of OC also has functions used to calculate the hash. The data storage location is determined by the hash value calculated by the data itself. When the content is the same in a timely manner, if the calculated hash value is different, the data storage location is also different]

5. Object Content Description

+ (NSString *) description; returns an NSSTring, indicating the content of the class to which the Message Receiver belongs. It is generally the class name of this class.

-(NSString *) description; returns an nsstring that indicates the content of the Instance Object of the message recipient. It is usually the class name and id value. You can also redefine the return value of description in the subclass.

6. Message sending mechanism

Selector and SEL type

The method name (selector) in the program will be replaced by an internal identifier after compilation. The data type corresponding to this internal identifier is the SEL type.

OC defines the @ selector () command to operate the compiled selector in the program. You can directly reference the compiled selector by using the @ selector () command. It can also be a variable of the SEL type.

The values of the SEL type generated after compilation and conversion vary depending on the selector. The values of the SEL type corresponding to the same selector must be the same.

 

 

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.