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:
- Value Object
- Collection
- 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.
- Notice
- Archiving and serialization
- Expressions and conditional judgments
- Objective-c Language Services
1.3 Uikit Framework applications can use Uikit to create interfaces in three ways
- Use the User interface tool (interface Buidler) to drag a window, view, or other object from the object library.
- Create with Code
- 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:
- @interface NSObject <NSObject> {
- Class Isa;
- }
Regardless of the role of the @interface keyword at compile time, you can put nsobject closer to the C language structure as:
- struct nsobject{
- Class Isa;
- }
Class is defined with a typedef.
- typedef struct Objc_class *class;
That NSObject can write like that.
- struct nsobject{
- Objc_class *isa
- }
What is the structure of the objc_class? This is probably the case:
- 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, 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:.
forwardinvocation: Allows one object to forward a message to another object.
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
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