Objective-c Data Type

Source: Internet
Author: User

The unique data types in Objective-c are listed here:


I. String

In Objective-c, A String constant is a string sequence enclosed by a pair of @ and a pair of quotation marks. For example, @ China and @ objective-c are all valid string constants.

 

Ii. id type
The id type is a unique type in Objective-c.
Conceptually, similar to the Object class in Java, it can be converted to any data type.
In other words, variables of the id type can store any data type object.
In terms of internal processing, this type is defined as a pointer to an object, which is actually a pointer to the instance variable of this object.

We can understand that the id type is the void * type. But they are not exactly the same. The definition of id in objc. h is as follows:

 

[Cpp]
  1. Typedef struct objc_object {
  2. Class isa;
  3. } * Id;
    As shown above, id is a pointer to struct objc_object. That is to say, id is an object that points to any class that inherits Ojbect (or NSObject. Note that id is a pointer, so you do not need to add a star number when using id. For example:

     

    Id n = nil;

    For example:

     

    [Cpp]
    1. Id data;
    2. Data = person;
    3. [Data print];
    4. ...
    5. Data = person1;
    6. [Data put];
    7. ...

       

      Iii. BOOL

      In objc. h, BOOL is defined:

       

      [Cpp]
      1. Typedef signed char BOOL;
      2. # Define YES (BOOL) 1
      3. # Define NO (BOOL) 0
        From the above definition, we can find that the values of Boolean variables are YES and NO, or 0 and 1.

         

        For example:

         

        [Cpp]
        1. BOOL enable = NO;
        2. Enable = 0;
        3. If (enable = YES )...
        4. If (enable )...
        5. If (! Enable )...

           

          It is similar to TRUE and FALSH in C.


          Iv. SEL
          In Objective-c, SEL is a type of selector.
          A selector is a pointer to a method. We can understand that the specified method will be executed when the program runs here.

          For example:

           

          [Cpp]
          1. SEL sel = @ selector (print );
          2. ClassA * classA = [[ClassA alloc] init];
          3. ClassB * classB = [[ClassB alloc] init];
          4. [ClassA initialize mselector: sel withObject: nil];
          5. [ClassB extends mselector: sel withObject: nil];
          6.  
          7. SEL selA = @ selector (print :);
          8. SEL selB = @ selector (print: age :);

            In the preceding example, sel is a pointer to the print method and does not specify the class to which it belongs. Therefore, the preceding method can be called when the print method is available in different classes.

            We can understand SEL as a function pointer, but they are not exactly the same.

             



            V. Class
            Similar to Java, you can use the Class to obtain the Class to which an object belongs. For example:
            Class theClass = [theObject class]; // obtain the class information of theObject object
            NSLog (@ Class Name: % s, [theClass className]); // Class Name of theObject
            The Class has several common methods, such as determining whether an object is an object of A Class (including a subclass:
            If ([theObject isKindOfClass: [Member class]) {…}
            If you do not want to include child classes, you can use:

            If ([theObject isMemberOfClass: [Member class] {…}

             

            Vi. nil and Nil

            Nil is the same as NULL in C. The definition in objc/objc. h is as follows:
            # Define nil 0/* id of Nil instance */

            Nil is a little different from nil. Nil defines a Class (Class rather than an object) pointing to null ). The specific definition is as follows:
            # Define Nil 0/* id of Nil class */

            For example:

             

            [Cpp]
            1. ClassA * classA = [[ClassA alloc] init];
            2. ClassA = nil
            3.  
            4. Id data;
            5. Data = nil;

               

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.