InObjective-CThere are some things we are not familiar with but often seeData TypeSuch as id, nil, Nil, SEL, etc. We have seen this in many articles.Data TypeBut it is not clear.
Objective-CSome interesting data types 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:
- // objc.h
-
- typedef struct objc_class *Class;
-
- typedef struct objc_object {
- Class isa;
- } *id;
-
- typedef struct objc_selector *SEL;
- typedef id (*IMP)(id, SEL, …);
- typedef signed char BOOL;
-
- #define YES (BOOL)1
- #define NO (BOOL)0
-
- #ifndef Nil
- #define Nil 0
- #endif
-
- #ifndef nil
- #define nil 0
- #endif
Here we will explain their details:
Id
Id and void * are not exactly the same. In the above Code, 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 that points to null, rather than an object ).
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:
- 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 name of the SEL method called), and some other parameters are added.
To put it bluntly, IMP is the implementation method.
Method
The objc/objc-class.h defines the type called Method as follows:
- typedef struct objc_method *Method;
- struct objc_method {
- SEL method_name;
- char *method_types;
- IMP method_imp;
- };
This definition seems to include other types we mentioned above. That is to say, the Method we often call) represents a type, which is related to selector and implementation (implementation.
Class
From the definition above, Class) is defined as a pointer to struct objc_class, which is defined in the objc/objc-class.h as follows:
- struct objc_class {
- struct objc_class *isa;
- struct objc_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;
- };
Summary: AboutObjective-CMediumData TypeI hope this article will help you with the introduction!