In objective-C, there are some data types that we are not familiar with but often see, such as ID, nil, nil, Sel, and so on. In many Article We have seen the introduction of these data types, but they are not clear.
This article starts with the bottom-layer definition and describes how these types are defined. This will help us gain a deeper understanding of objective-C.
The original Article is Greg Miller. The article address is:
Http://unixjunkie.blogspot.com/2006/02/nil-and-nil.html
Some interesting data types in objective-C are often incorrectly understood. Most of them can be found in/usr/include/objc. h or other header files in this directory. The following section is extracted from objc. h and defines some data types:
1. // objc. h
2.
3. typedef struct objc_class * class;
4.
5. typedef struct objc_object {
6. Class ISA;
7.} * ID;
8.
9. typedef struct objc_selector * sel;
10. typedef ID (* IMP) (ID, Sel ,...);
11. typedef signed Char bool;
12.
13. # define Yes (bool) 1
14. # define no (bool) 0
15.
16. # ifndef Nil
17. # define nil 0/* ID of Nil class */
18. # endif
19.
20. # ifndef Nil
21. # define nil 0/* ID of Nil instance */
22. # endif
Here we will explain their details:
ID
ID and void * are not exactly the same. Above Code In, ID is a pointer to struct objc_object, which basically means that ID is an object pointing to any one that inherits the object (or nsobject) class. Note that ID is a pointer, so you do not need to add a star number when using ID. For example, Id Foo = nil defines an Nil pointer, which points to any subclass of nsobject. Id * Foo = nil defines a pointer, which points to another pointer and points to a subclass of nsobject.
Nil
Nil is the same as null in C, which is defined in objc/objc. h. Nil indicates an Objctive-C object. The pointer of this object points to null (nothing is null ).
Nil
Nil and nil in the upper-case letters are a little different. Nil defines a class (class rather than an object) pointing to null ).
Sel
This is very interesting. SEL is a type of "selector", indicating the name of a method. For example:
-[Foo count] and-[Bar count] use the same selector. Their selector is called count.
In the header file above, we can see that SEL is a pointer to struct objc_selector, but what is objc_selector? In fact, when you use the GNU objective-C Runtime Library and the next objective-C Runtime Library (Mac OS X uses the next Runtime Library, their definitions are different. Mac OSX only maps sel to a C string. For example, if we define a foo class with a-(INT) Blah method, the following code:
1. nslog (@ "sel = % s", @ selector (blah ));
The output is Sel = blah.
To put it bluntly, Sel is the name of the returned method.
IMP
From the header file above, we can see that IMP is defined as ID (* IMP) (ID, Sel ,...). In this case, IMP is a pointer to a function. The directed function includes the ID ("self" pointer), The called sel (method name), and some other parameters.
To put it bluntly, IMP is the implementation method.
Method
The objc/objc-class.h defines the type called method as follows:
1. typedef struct objc_method * method;
2. struct objc_method {
3. Sel method_name;
4. char * method_types;
5. IMP method_imp;
6 .};
This definition seems to include other types we mentioned above. That is to say, method (the method we often call) represents a type, which is related to selector and implementation (implementation.
Class
From the definition above, class (class) is defined as a pointer to struct objc_class, which is defined in the objc/objc-class.h as follows:
1. struct objc_class {
2. struct objc_class * ISA;
3. struct objc_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 .};