Introduction to the Cocoa framework For iOS (I) framework, parsing of Objectivie-C Runtime capabilities, etc.

Source: Internet
Author: User
Tags print object
Document directory
  • 1.1. Cocoa framework
  • 1.2. Foundation framework
  • 1.3 UIKit framework
  • 2.1 Objective-C is an object-oriented language
  • 2.2 advantages of Objective-C
  • 2.3 dynamic capability-related isa pointer
  • 2.4 NSObject
  • 2.5 Cocoa object Lifecycle

The Cocoa framework is the basis of iOS applications. Understanding the Cocoa framework is of great help for developing iOS applications.

1. What is Cocoa?

Cocoa is the operating environment for OS X and iOS operating systems.

What makes a program a Cocoa program? It is not a programming language, because you can use a variety of languages in Cocoa development; it is not a development tool, you can create a Cocoa program on the command line. The Cocoa program can say that it is composed of some objects, and the classes of these objects are ultimately inherited from their root class: NSObject. They are all based on the Objective-C runtime environment.

1.1. Cocoa framework

In iOS, the two most important and basic frameworks of Cocoa are Foundation and UIKit.

The Foundation has nothing to do with the interface. It can also be said that the class that has nothing to do with the interface is basically the Foundation framework, and the interface is related to the UIKit framework.

The positions of these two frameworks in the system

1.2. Foundation framework

Well, let's take a look at the class organization architecture diagram of the two frameworks. The first one is to look at the Foundation, three diagrams, including the Foundation class. The gray graph is not supported by iOS, the gray part belongs to the OS X system.

Perform logical classification of classes in the Foundation framework as follows:

  1. Value Object
  2. Set
  3. Operating system services include the following: File System and URLInter-process communication.Most classes in this category represent different system ports, sockets, and name servers, which are useful for implementing the underlying IPC. NSPipe represents a BSD pipe, that is, a one-way communication channel between processes.Threads and subtasks.The NSThread class allows you to create multi-threaded 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 for other work or progress monitoring.
  4. Notification
  5. Archiving and serialization
  6. Expression and condition judgment
  7. Objective-C Language Service
1.3 The UIKit framework application can use the UIKit Creation Interface in three ways
  1. Drag a window, view, or other objects from the object library using the interface Buidler.
  2. Create with code
  3. Customize the user interface by inheriting the UIView class or indirectly inheriting the UIView class
Framework Structure:

As shown in 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 you scroll the list with your fingers or input it on the virtual keyboard, UIKit generates a time and sends it to the UIResponder response chain until there are objects in the chain to process this event. The corresponding core objects, such as UIApplication, UIWindow, and UIView, are inherited directly or indirectly from UIResponder.

2. Cocoa object 2.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 heavy-load operations, templates, and multi-inheritance, nor does it have Java garbage collection mechanism.

2.2 advantages of Objective-C

Objective-C language has the characteristics of object-oriented such as C ++ Java, which far does not reflect its advantages. The advantage of Objective-C is that it is dynamic. There are three types of dynamic capabilities:

Dynamic class-determines the class object during runtime

Dynamic binding-determine the method to be called during running

Dynamic Loading-new modules are loaded for the program at runtime

2.3 dynamic capability-related isa pointer

Each Objective-C object has a hidden data structure, which is the first member variable of the Objective-C object. It is the isa pointer. Where does this pointer point? It points to a class object (class object remembers that it is an object and a variable that occupies memory space. This object is generated by the compiler during compilation, specifically to describe the definition of a class). This class object contains some information about the Objective-C object (to distinguish two objects, I call the previously mentioned object Objective-C object ), including the method scheduling table of the Objective-C object and the Protocols implemented. This information is the root cause of Objective-C dynamic capabilities.

Let's look at the data structure of the isa pointer type? If other member data and variables of the NSObject object are put aside, NSObject can be viewed as follows:

@interface NSObject <NSObject> {     Class    isa;} 

Without considering the function of the @ interface keyword during compilation, NSObject can be closer to the C language structure and expressed:

struct NSObject{   Class isa; }

Class is defined by typedef.

typedef struct objc_class *Class;

So NSObject can be written like this.

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 we can see that there is also an isa pointer in this struct, which is another important point. Does it feel like it is a dream space. Don't be nervous, take easy, there won't be so many layers, here the isa Pointer Points to the metaclass object, with a metaclass word, it proves that it is coming soon. What is the use of the object? It is used to store information about the class version, name, class method, and so on. All metaclass objects point to NSObject metadata objects. Three times in total: Class Object> Meta Object> NSObject meta object.

To obtain information about the entire Class organizational structure, the objc_class structure defines the second member variable Class super_class, which points to the Class Object of the parent Class. After talking so much, the relationship may be unclear. It is a map that is better than a thousand words.

As shown in the figure, D3 inherits D2, D2 inherits D1, and D1 finally inherits NSObject. From an object of D3, arrange the relationship among D3 D2 D1 NSObject class objects and metadata class objects.

The arrows in the figure are Pointer Points.

2.4 NSObject

NSObject is the root class of most Objective-C classes. It does not have a parent class. Other classes inherit NSObject and access the basic interface of the Objective-C runtime system, so that instances of other classes can obtain the runtime capability.

2.4.1 root and root protocols

NSObject is not only a class name. NSObject is also a protocol name. Refer to the NSObject protocol, the NSObject Protocol specifies the interface that must be implemented by the root class.

2.4.2 main methods of the root class:
  • Allocation, initialization, and replication:

Alloc and allocWithZone: The method is used to allocate an object memory from a memory area and point the object to its class definition during runtime.
The init method is object initialization.
New is a method that combines simple memory allocation and initialization.
Copy and copyWithZone:

  • Object Persistence and cleanup:

The retain method increases the number of Object Persistence times.
The release method reduces the number of Object Persistence times.
The autorelease method also reduces the number of Object persistence, but delays the operation.
The retainCount method returns the current number of Hold times.
The dealloc method is implemented by the instance variables of the objects to be released and the classes that release the dynamically allocated memory.

  • Introspection and Comparison

NSObjec has many methods to query the runtime information of an object. These introspection methods help to locate the object in the class hierarchy, determine whether the object implements a specific method, and test whether the object complies with a certain protocol. The following are some methods:
The superclass and class methods (implemented as Class and instance methods) return the parent class and Class of the receiver in the form of class objects respectively.
You can use isKindOfClass: And isMemberOfClass: To determine which class the object belongs. The latter is used to test whether the receiver is an instance of the specified class. IsSubclassOfClass: the class method is used to test the inheritance of the class.
RespondsToSelector: This method is used to test whether the receiver implements a method identified by the selector parameter. InstancesRespondToSelector: class method 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) complies with the given protocol.
IsEqual: Compared with the hash Method for objects.
The description method allows the object to return a content description string. The output of this method is often used for debugging (the "print object" command) and represents the object together with the "% @" indicator in the format string.

  • Object encoding and decoding

The following method is related to the object encoding/decoding (as part of the archiving process:
EncodeWithCoder: And initWithCoder: are the only NSCoding method. The former enables the object to encode its instance variables, and the latter enables the object to initialize itself based on decoded instance variables.
NSObject class declares some methods related to object encoding: classForCoder:, replacementObjectForCoder:, and awakeAfterUsingCoder :.

  • Message forwarding

 ForwardInvocation: allows an object to forward messages to another object.

  • Message Distribution

Some methods starting with performSelector allow you to distribute specified messages after delay, and send messages (synchronous or asynchronous messages) from the auxiliary thread to the main thread.

2.5 Cocoa object Lifecycle

Shows four memory management methods for an object.

  • Object lifecycle-Simplified View

  • Keep the received object

  • Copy the received object

  • Auto Release pool

Refer:

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

Rong Fangzhi (http://blog.csdn.net/totogo2010)

This article follows the "signature-non-commercial use-consistency" creation public agreement

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.