Objective-c object mode in-depth analysis

Source: Internet
Author: User

The objective-c object Modelas a objective-c software developer, you can still do a lot of work without having to understand Objective-c's originator C language and the object model adopted by OBJECTIVE-C. In fact, because these features are really complex, it can be daunting to learn these object models. Although the problem is complex, there is a need to try to learn it, as you will eventually need to deal with the Objective-c runtime, and at that time understand that the object model will help you to better solve such problems. take a cup of tea, or a cup of coffee, and follow me along to explore Objective-c's object model.
Object is an instance of a classIt seems obvious, but we might as well take some time to think about it. When we say, "an object is an instance of a class," This sentence means something. Illustrate this in the following code.
NSString *name = [NSString stringwithformat:@ "%@", @ "Peter"];
This code looks more like a man-made rather than a piece of code that actually needs to be used, but it is important to show how the OBJECTIVE-C runtime works. When the above code is finished, we finally get a variable called name , which is an instance of the NSString class, consider the following:
    • An object (an instance of a class) is used to hold the state.
    • The state of an object is persisted within its instance variable. For example, the name variable in the above code allocates some memory to hold the string content "Peter"
    • The name variable also contains a pointer to the class (NSString) in which it resides
    • Inside the objective-c, the object contains no methods (behaviors) and only holds the state information
    • The class contains only methods and does not contain any state information. These include methods that their instances can respond to (that is, instance methods)
    • To make it clear, let's repeat: The object contains the saved state (that is, the instance variable), and the class contains the saved behavior (method)
To make the presentation more specific, let's look at the following icons:
Example method diagramThe blue box indicates the name variable (an instance of class NSString). As shown, let's take a look at what happens if you send a length message to the variable name .
Nsuinteger len = [name length];
    1. First OBJECTIVE-C will ask the variable name which class is the instance, because all the methods are contained within the class, so from there can begin to find the method length where.
    2. Because name is an instance variable of class NSString , Objective-c will first find the class NSStringand then begin to find the method length.
    3. If Objective-c finds the method lengthinside the class NSString , it stops finding and starts executing the method. Otherwise, it will continue to find based on the inheritance relationship of the class.
It is important to note that the process described above has been simplified by me, and I have not considered other scenarios where the method is stored. (such as the classification of objective-c)

Class is also an objectsince the class is able to save its instance methods ( instance methods ), where does the class method persist? Let's take a look at the code that creates the variable name :
NSString *name = [NSString stringwithformat:@ "%@", @ "Peter"];
in the above code, the message stringWithFormat: is sent to the class nsstring . Can a class receive messages? If so, where are those methods stored? In fact, Objective-c's grammar hides something, and he hides the fact that the class is also the object . if the class nsstring is an object, it means that it must be instantiated from a class that holds its method. The speculation is right, but before we dive into this, let's look at a bit of code:
@interface Person:nsobject {}+ (void) aclassmethod;-(void) Aninstancemethod; @end
Maybe you've seen a lot of this kind of code, but what does this code do?
  • declares a class that is a Person
  • This class is used to save instance methods
  • There is another class created behind this code, but unfortunately this extra class name is also a person, but it's a people meta-class .
  • Person the Meta class holds the class method of the person
It seems that the problem is getting more and more complicated, because the same name is used over and over again, back to the original variable name to discuss, we can say:
  • variables name is a class NSString an instance of
  • class NSString is a NSString an object instantiated by a meta-class
  • If you send a message to the object name length , then Objective-c would go to class NSString Find this method
  • If you send an object NSString Send stringwithformate: message, Objective-c will be in NSString meta-class to find this method
  • NSString is a class, also an object, and a class is also an object
here's another class diagram .
meta-class method (class method) Diagram This time, the blue box represents the nsstring object, and let's take a look at The code that creates the name object:
NSString *name = [NSString stringwithformat:@ "%@", @ "Peter"];
when sending a message to the object NSString stringwithformate: , Objective-c will:
    1. < Span style= "font-size:14px" > ask nsstring Span style= "COLOR: #171714" The object is an instance of which class, which is its meta-class
    2. start at nsstring stringwithformate: If not found, it continues to look for
    based on the class's inheritance relationship
of course, since NSString inherits from NSObject, the nsstring Meta class inherits from NSObject Meta class.
Strange nsobjecT-inheritance If you're still sober, take advantage of what you now know about the Objective-c object model to see what happens to your code as it runs. There may be one more question you want to know: for what nsobject Meta class seems to inherit from the nsobject class? That's because it's the way it is. consider the following scenarios:
  • NSObject Save the method for its instance (save instance method)
  • NSObject Meta class for class NSObject Save the method. (Save class method)
  • NSObject Meta class inherits from non-meta class NSObject
  • therefore, NSObject instance methods are also class methods, which are found in the lookup path of all objective-c meta-class methods
It's a bit strange, but if you look at the picture above, you should be clearer.

Objective-c object mode in-depth analysis

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.