IOS overall framework class diagram is worth collecting

Source: Internet
Author: User
Tags print object uikit

The Cocoa framework is the foundation of iOS apps, and understanding the cocoa framework is a great help for developing iOS apps.

1. What is cocoa?

Cocoa is the operating environment for OS X and iOS operating system programs.

What factors make a program a cocoa program? is not a programming language, because you can use a variety of languages in cocoa development, and not a development tool, you can create cocoa programs on the command line. Cocoa program so to speak, it is composed of some objects, and the classes of these objects are finally inherited from their root class: NSObject. And they are all based on the OBJECTIVE-C operating environment.

1.1. Cocoa Frame

iOS, cocoa the most important and basic two frameworks in many frameworks are: Foundation and UIKit.

Foundation and interface-Independent, can also say and interface-independent classes are basically the foundation framework, and interface-related is the Uikit framework.

Where these two frames are located in the system

1.2. Foundation Framework

Well, let's take a look at the two framework of the class organization chart, the first to see the foundation, three graphs, including the foundation so the class, the gray is not supported by iOS, the gray part of the OS X system.

The classes in the foundation framework are logically categorized as follows:

    1. Value Object
    2. Collection
    3. Operating system services include the following three: File system and URL interprocess communication. most of the classes in this category represent different system ports, sockets, and name servers, and are useful for implementing the underlying IPC.   Nspipe represents a BSD pipeline, a one-way communication channel between processes. threads and sub-tasks. the Nsthread class allows you to create multithreaded programs, while various lock classes provide a variety of control mechanisms for competing threads when accessing process resources. With Nstask, your program can separate a sub-process to perform other work or monitor progress.
    4. Notice
    5. Archiving and serialization
    6. Expressions and conditional judgments
    7. Objective-c Language Services

1.3 Uikit Framework applications can use Uikit to create interfaces in three ways
    1. Use the User interface tool (interface Buidler) to drag a window, view, or other object from the object library.
    2. Create with Code
    3. Implement a custom user interface by inheriting the UIView class or indirectly inheriting the UIView class
Framework Class Organization chart:

As can be seen in the figure, the responder class is the root class of the largest branch in the graph, and Uiresponder defines the interface and default behavior for handling response events and response chains. When the user scrolls through the list with a finger or enters on a virtual keyboard, the Uikit generates time to transmit to the uiresponder response chain until an object in the chain handles the event. The corresponding core objects, such as: UIApplication, Uiwindow,uiview are inherited directly or indirectly from Uiresponder.

2. Cocoa Object 2.1 Objective-c is an object-oriented language

Objective-c, like Java C + +, has encapsulation, inheritance, polymorphism, and reuse. However, unlike C + +, it has overloaded operations, templates and multiple inheritance, and no Java garbage collection mechanism.

Advantages of 2.2 Objective-c

Objective-c language has C + + Java and other object-oriented features, it is far from the advantages of the expression. The advantage of objective-c is that it is dynamic. There are three types of dynamic capabilities:

Dynamic class-The runtime determines the object of the class

Dynamic binding-The runtime determines which method to invoke

Dynamic load--load a new module for a program at run time

2.3 Dynamic capability-related ISA pointers

Each Objective-c object has a hidden data structure, which is the first member variable of the Objective-c object, which is the ISA pointer. Where does this pointer point? It points to a class object that remembers it as an object, a variable that takes up memory space, which is generated by the compiler at compile time, specifically to describe the definition of a class, which contains some information about the Objective-c object (in order to distinguish two objects, I refer to the previous object called Objective-c object), including the Objective-c object's method scheduling table, the implementation of what protocol and so on. The inclusion of information is the source of objective-c dynamic capabilities.

Let's look at the data structure of the ISA pointer type. If you throw away other member data and variables of the NSObject object, NSObject can look like this:

    1. @interface NSObject <NSObject> {
    2. Class Isa;
    3. }

Regardless of the role of the @interface keyword at compile time, you can put nsobject closer to the C language structure as:

    1. struct nsobject{
    2. Class Isa;
    3. }

Class is defined with a typedef.

    1. typedef struct Objc_class *class;

That NSObject can write like that.

    1. struct nsobject{
    2. Objc_class *isa
    3. }

What is the structure of the objc_class? This is probably the case:

  1. struct Objc_class {
  2. Class Isa;
  3. Class Super_class;
  4. Const Char *name;
  5. long version;
  6. long Info;
  7. long instance_size;
  8. struct objc_ivar_list *ivars;
  9. struct objc_method_list **methodlists;
  10. struct Objc_cache *cache;
  11. struct objc_protocol_list *protocols;
  12. }

Here you will see, there is an Isa pointer in this structure, but also a heavy point, is not a kind of stolen dream space feeling. Don't be nervous, take the easy, there will not be so many levels, here the ISA pointer to the meta-class objects (Metaclass object), with the meta-word, proving that the end of the end. What's the use of the meta-object? It is used to store information about the class's version, Name, class method, and so on. All meta-class objects (Metaclass object) point to NSObject's meta-class object, either to the head or to the nsobject. Three times: Class Object---class object->nsobject meta-class object.

To get information about the entire class structure, the second member variable class Super_class is defined in the Objc_class struct, which points to the class object of the parent class. Say so much, maybe the relationship is unclear, saying goes a picture is more than words

As can be seen in the figure, D3 inherits D2,d2 inheritance D1,d1 eventually inherits NSObject. Starting from an object in D3, the D3 D2 D1 nsobject class object, Meta-class object, etc. are arranged.

The arrows in the diagram are pointing to the pointer.

2.4 Root Class NSObject

NSObject is the root class of most objective-c classes, and it does not have a parent class. Other classes inherit nsobject, accessing the basic interface of the OBJECTIVE-C runtime system so that instances of other classes can gain runtime capabilities.

2.4.1 root class and root class protocol

NSObject is not only a class name, NSObject is also the name of the protocol, referring to the NSObject protocol, the NSObject protocol specifies the interface that the root class must implement.

2.4. The main methods of the 2 root classes:
    • Assign, initialize, and replicate:

Alloc and Allocwithzone: methods are used to allocate an object memory from a memory area and to point the object to its runtime class definition.
The Init method is object initialization.
New is a method of combining simple memory allocation and initialization.
Copy and Copywithzone:

    • Object Retention and cleanup:

The Retain method increases the number of times the object is persisted.
The release method reduces the number of objects held.
The Autorelease method also reduces the number of objects to hold, but in a deferred manner.
The Retaincount method returns the current number of holds.
The Dealloc method is implemented by classes that need to dispose of the object's instance variables and release dynamically allocated memory.

    • Introspection and comparison

Nsobjec There are many ways to query the run-time information of an object. These introspective methods help to find out where an object is located in the class hierarchy, determine whether an object implements a particular method, and whether the test object follows a protocol. Here are some of the methods
The Superclass and class methods (implemented as class and instance methods) return the recipient's parent classes and classes in the form of a class object, respectively.
You can determine which class the object belongs to by Iskindofclass: and Ismemberofclass: Methods. The latter is used to test whether the recipient is an instance of the specified class. Issubclassofclass: Class methods are used to test the inheritance of classes.
Respondstoselector: Method is used to test whether the receiver implements the method identified by the selector parameter. Instancesrespondtoselector: The class method is used to test whether an instance of a given class implements the specified method.
Conformstoprotocol: Method is used to test whether the recipient (object or Class) follows a given protocol.
IsEqual: And hash methods are used for object comparisons.
The description method allows an object to return a content description string, and the output of this method is often used for debugging (the "Print Object" command), and for representing the object in a formatted string along with the "%@" indicator.

    • Encoding and decoding of objects

The following methods and objects are encoded (as part of the archive process):
Encodewithcoder: And Initwithcoder: Is the only method nscoding protocol. The former enables an object to encode its instance variables, which enables an object to initialize itself based on the decoded instance variable.
Some methods related to object encoding are declared in the NSObject class: Classforcoder:, Replacementobjectforcoder:, and Awakeafterusingcoder:.

    • Forwarding of messages

forwardinvocation: Allows one object to forward a message to another object.

    • Distribution of messages

Some of the methods at the beginning of performselector allow you to distribute the specified message after a delay, and you can distribute the message (synchronous or asynchronous) from the worker thread to the main path.

2.5 Cocoa Object Life cycle

Four ways to manage memory for objects, as shown in

    • Life cycle of objects-simplified view

    • Keep the Received object

    • Copy the Received object

    • Auto Free Pool

Reference:

1, Http://algorithm.com.au/downloads/talks/objective-c-internals/objective-c-internals.pdf

2, http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/cocoafundamentals/introduction/introduction.html

3, http://www.cnblogs.com/csutanyu/archive/2011/12/12/Objective-C_memory_layout.html

Ext.: http://blog.csdn.net/fengsh998/article/details/8842885

IOS overall framework class diagram is worth collecting

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.