IOS overall framework and class inheritance framework diagram

Time:2022-5-7

I. overall framework

In the process of IOS development, understanding and learning the overall framework of IOS is an essential link. Today, let’s have a good understanding of the overall framework of IOS. First, post a framework introduction about IOS:Overview of IOS system framework。 We usually call the IOS framework cocoa touch framework. Cocoa touch is a collection of frameworks, which contains many sub frameworks. Each subframe is a directory, including a shared resource library, which is used to access the header files of the code stored in the resource library, as well as other resources such as images and sound files. The shared resource library defines the functions and methods that can be called by the application. The classes in the framework are interdependent to form a whole and provide perfect services or solutions in one aspect. Multiple frameworks work together to realize the structure of the whole application. Because the structure of the application program is universal, developers can do detailed and personalized processing through the functions and methods provided by the framework, so as to meet the different needs of different applications. To develop an application is to insert the detailed code into the design provided by the framework to form a whole to complete the final application design.

1.1 IOS system architecture

The overall system framework of IOS is shown in the figure below. The system architecture of IOS is mainly composed of
Application layer
Cocoa touch layer
Media layer
Core services layer
Core OS layer
The kernel and device drivers layer.

IOS overall framework and class inheritance framework diagram

After understanding the system architecture, what are the main functions of each layer? The following figure is ios8 3. The latest diagram of the system framework is not found. Now some functional modules have been added to IOS 11, but the overall framework is similar.

IOS overall framework and class inheritance framework diagram
  • Cocoa Touch Layer
    The touch layer provides the key technical support of the application foundation and the appearance of the application. Such as notification center’s local notification and remote push service, IAD advertising framework, gamekit game tool framework, message UI framework, picture UI framework, map framework, connection watch framework, UIKit framework, automatic adaptation, etc

  • Media Layer
    The media layer provides audio-visual technologies in applications, such as coregraphics, coreimage, glkit, OpenGL es, coretext, imageio, etc. Coreaudio, openal, avfoundation related to sound technology, coremedia related to video, media player framework, airplay framework for audio and video transmission, etc

  • Core Services Layer
    The system service layer provides the basic system services required by the application. Such as accounts account framework, advertising framework, data storage framework, network connection framework, geographic location framework, motion framework and so on. The core of these services is the core foundation and foundation framework, which define the data types used by all applications. Corefoundation is a set of interfaces based on C. foundation is the OC encapsulation of corefoundation

  • Core OS Layer
    The system core layer contains most low-level functions close to hardware, and the framework it contains is often used by other frameworks. The accelerate framework contains interfaces for digital signals, linear algebra and image processing. Optimize the hardware differences of all IOS devices to ensure that the code written once runs efficiently on all IOS devices. The core Bluetooth framework uses Bluetooth to interact with peripherals, including scanning and connecting Bluetooth devices, saving connection status, disconnecting, obtaining peripheral data or transmitting data to peripherals, etc. The security framework provides security related solutions such as certificate management, public and private key trust policy, keychain, hash authentication, digital signature and so on.

Each of the above modules only briefly introduces the general functions. For more details, you can click here to see the great God’s blog to learn more:Introduction and detailed description of IOS overall framework。 Obviously, of all the above frameworks, the most important and frequently used one isUIKitandFoundationFrame.

1.2 official documents

Official document stamp here!!
Explain, the official document divides the OS system into four layers, as shown in the figure below. Instead of cocoa touch layer and application layer, it is replaced by cocoa (application) layer. Let’s take a closer look at the official explanation of the cocoa (application) layer on the official website as follows. It says that this layer provides a user interface basis for creating apps, responds to user events, and can manage app behavior. This is actually the role of cocoa touch layer (UIKit), but the application layer is omitted from the official website, because this layer is mainly app, which is mainly completed by developers. Other materials can be viewed according to the official documents. There is no more interpretation and analysis here.

  • The Cocoa (Application) layer includes technologies for building an app’s user interface, for responding to user events, and for managing app behavior.
IOS overall framework and class inheritance framework diagram

Class inheritance system of cocoa framework

Cocoa framework is the basis of IOS applications and the running environment of OS X and IOS operating system programs. Understanding cocoa framework is very helpful to develop IOS applications. In IOS, the two most important and basic frameworks among cocoa’s many frameworks are foundation and UIKit. These two frameworks basically include the vast majority of OC classes used in the development of IOS and OS X. therefore, these two frameworks are cocoa frameworks. Among them, foundation is independent of the interface. It can also be said that classes independent of the interface are basically foundation framework, and UIKit framework is related to the interface
Previously, when learning Java and Android, Java and Android provided a very detailed system framework, and the class inheritance system and classification of Java and Android were very clear in the API index. However, in the IOS development process, the aggregation of classes in the API was divided into modules according to functions and functions, and there was no clear introduction to the class inheritance system in IOS. The understanding of class inheritance system helps us to have a clearer understanding and understanding of the system framework of the whole module of IOS system, and have a general understanding and understanding of the characteristics and general usage of classes in the corresponding class inheritance system. Therefore, in these two days of leisure, I sorted and studied this piece, and combed the class inheritance system in the most important foundation and UIKit framework in the cocoa framework.

2.1 foundation framework class inheritance system

The foundation framework provides basic system services for all applications. Applications, as well as UIKit and other frameworks, are built on the infrastructure of the foundation framework. The foundation framework provides many basic object classes and data types, making it the basis of application development. It also makes some conventions (such as for tasks such as canceling allocation) to make the code more consistent and reusable.

Without further ado, let’s take a look at the foundation framework. There are three figures, including all classes of foundation. In the figure, the gray part is not supported by IOS, and the gray part is OS X system.

IOS overall framework and class inheritance framework diagram
IOS overall framework and class inheritance framework diagram
IOS overall framework and class inheritance framework diagram

Logically classify the classes in the above foundation framework as follows:

  1. Value object
  2. aggregate
  3. Operating system services include the following three
    • File system and URL
    • Interprocess communication.Most classes in this category represent different system ports, sockets, and nameservers, which are very useful for implementing the underlying IPC. Nspipe represents a BSD pipeline, that is, a one-way communication channel between processes.
    • Threads and subtasks.The nsthread class enables you to create multithreaded programs, while various lock classes provide various control mechanisms for competing threads to access 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. Expression and conditional judgment
  7. Objective-C language service

For the usage of some basic classes in the foundation framework, see:IOS development series – foundation framework of Objective-C

2.2 UIKit framework class inheritance system

Uikitk framework provides a series of classes to establish and manage the user interface (UI) interface, application object, event control, drawing model, window, view and interface for controlling touch screen of IOS application. Entry of framework: import < UIKit / UIKit h> , when this header file is introduced, any class declared in UIKit can be used in the program. PS: of course, link this framework to your application.

  • (PS1: it can be considered as an API library for manipulating the interface)
  • (PS2: UIKit framework is used iniOSThe corresponding application kit on the platform is the application kit on Mac OS X. the two are sister frameworks, and their functions and purposes are not much different (I didn’t say that the process of achieving the purpose is the same), and the table is confused)

Applications can use UIKit to create interfaces in three ways:

  1. Drag and drop windows, views or other objects from the object library in the interface builder.
  2. Create with code
  3. Customize the user interface by inheriting the uiview class or indirectly inheriting the uiview class

Similarly, the class inheritance system diagram of uikitk framework is shown in the following figure:

IOS overall framework and class inheritance framework diagram

As can be seen from the figure, the responder class is the root class of the largest branch in the figure. Uiresponder defines the interface and default behavior for handling response events and response chains. When the user scrolls the list with his finger or enters on the virtual keyboard, UIKit generates time and transmits it to the uiresponder response chain until an object in the chain handles the event. The corresponding core objects, such as uiapplication, UIWindow and uiview, inherit directly or indirectly from uiresponder. Brief introduction of various classes of the UIKit framework and the links behind the stamp:**Brief description of each class of UIKit framework **

Three cocoa objects

The content here comes from:IOS overall framework class diagram is worth collecting

3.1 Objective-C is an object-oriented language

Like Java C + +, Objective-C has encapsulation, inheritance, polymorphism and reuse. However, unlike C + +, it does not have overloaded operation method, template and multi inheritance, nor does it have Java garbage collection mechanism.

3.2 advantages of Objective-C

Objective-C language has the characteristics of object-oriented such as C + + Java, which is far from reflecting its advantages. 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 the method to call
  • Dynamic loading——The runtime loads new modules for the program

3.3 isa pointer related to dynamic capability

Each Objective-C object has a hidden data structure. This data structure 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 (remember that class object is an object and a variable occupying memory space. This object is generated by the compiler during compilation to describe the definition of a class). This class object contains some information of Objective-C objects (in order to distinguish the two objects, I call the object mentioned above Objective-C object), including the method scheduling table of Objective-C objects, What protocols have been implemented and so on. This information is the root of Objective-C dynamic capability. Let’s take a look at the data structure of ISA pointer type?

If other member data and variables of the nsobject object are discarded, the nsobject can be regarded as:

@interface NSObject <NSObject> {  
     Class    isa;  
}

Regardless of the role of @ interface keyword in compilation, nsobject can be expressed as closer to C language structure:

struct NSObject{  
   Class isa;  
 }

Class is defined with typedef:typedef struct objc_class *Class;Then nsobject can write like this

struct NSObject {  
  objc_class *isa  
}

That’s objc_ What is the structure of class? It’s probably like this:

struct objc_class {  
     Class isa;  

     Class super_class;  

     const char *name;  

     long version;  
     long info;  

     long instance_size;  
     struct objc_ivar_list *ivars;  
     struct objc_method_list **methodLists;   

     struct objc_cache *cache;  
     struct objc_protocol_list *protocols;     
 }

Here you will see that there is also an ISA pointer in this structure, which is a double direction. Does it feel like stealing dream space. No need to prove that there are so many metaclasses with metaclasses. There won’t be so many pointers to objects. What’s the use of meta objects? It is used to store information about the version, name, class method and so on. All metaclass objects point to the metaclass object of nsobject, which is still nsobject in the end. Three times in total: class object – > metaclass object – > nsobject metaclass object.

In order to get the information of the whole class organization structure, objc_ The second member variable class Super is defined in the class structure_ Class, which points to the class object of the parent class. Having said so much, the relationship may not be clear. Youdao is a picture more than a thousand words

IOS overall framework and class inheritance framework diagram

3.4 root class nsobject

Nsobject is the root class of most Objective-C classes. It has no parent class. Other classes inherit nsobject and access the basic interface of Objective-C runtime system, so that instances of other classes can obtain runtime capabilities.

  • Root class and root class protocol: nsobject is not only a class name, but also a protocol name,Refer to nsobject protocol, the nsobject protocol specifies the interface that the root class must implement.
  • Main methods of root class:Allocate, initialize, copy
    • Allocation: alloc and allocwithzone: methods are used to allocate an object’s memory from a memory area and point the object to its runtime class definition.
    • Initialization: the init method is object initialization.
    • New is a method that combines simple memory allocation with initialization.
    • Copying: copy and copywithzone
  • Object retention and cleaning:
    • The retain method increases the number of times an object is held.
    • The release method reduces the number of times an object is held.
    • The autorelease method also reduces the number of times the object is held, but in a delayed manner.
    • The retaincount method returns the number of times to hold the current.
    • Dealloc method is implemented by the class that needs to release the instance variable of the object and release the dynamically allocated memory.
  • Nsobjec has many methods to query the runtime information of objects. These introspective methods help to find the position of the object in the class hierarchy, determine whether the object implements a specific method, and test whether the object follows a protocol. Here are some methods
    • Superclass and class methods (implemented as classes and instance methods) return the parent class and class of the receiver in the form of class objects respectively.
    • Iskindofclass: and ismemberofclass: methods to determine which class the object belongs to. The latter is used to test whether the receiver is an instance of the specified class. Issubclassofclass: the class rule is used to test the inheritance of a class.
    • Respondstoselector: the method is used to test whether the receiver implements the method identified by the selector parameter. Instancesrespondtoselector: the class rule is used to test whether the instance of a given class implements the specified method.
    • Conformstoprotocol: the method is used to test whether the receiver (object or class) follows the given protocol.
    • Isequal: and hash methods are used for object comparison.
    • The description method allows the object to return a content description string; The output of this method is often used for debugging (“print object” command) and to represent the object in the format string together with the “% @” indicator.
  • The following methods are related to the encoding and decoding of objects (as part of the archiving process):
    • Encodewithcoder: and initwithcoder: are the only methods of nscoding protocol. The former enables the object to encode its instance variables, while the latter enables the object to initialize itself according to the decoded instance variables.
    • Nsobject class declares some methods related to object coding: classforcoder:, replacementobjectforcoder:, and awakeafterusingcoder:.
  • Message forwarding
    • Forwardinvocation: allows one object to forward messages to another object.
  • Distribution of messages
  • Some methods at the beginning of performselector allow you to dispatch specified messages after delay, and you can dispatch messages (synchronous or asynchronous messages) from the worker thread to the main thread.

Link:https://www.jianshu.com/p/28428b90b07a

Recommended Today

Wanandroid open source project

Quickly create an Android client with good experience. The original version is Java, and the current project is a mixed version with clear hierarchy and clear annotations. The project stability and UI have been greatly improved compared with the initial version. If you still feel good, just order a star (the project is continuously upgrading) […]