Introduction to Data Types in Objective-C

Source: Internet
Author: User

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:

 
 
  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    
  18. #endif  
  19.    
  20. #ifndef nil  
  21. #define nil 0    
  22. #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:

 
 
  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 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:

 
 
  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, 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:

 
 
  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. }; 

Summary: AboutObjective-CMediumData TypeI hope this article will help you with the introduction!

Related Article

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.