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:
typedef struct objc_object { Class isa; }*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:
id data; data = person; [data print]; … data = person1; [data put]; …
Iii. bool
In objc. H, bool is defined:
typedef signed char BOOL; #define YES (BOOL)1 #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:
BOOL enable=NO; enable=0; if(enable == YES) … if(enable) … 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:
SEL sel=@selector (print); ClassA *classA = [[ClassA alloc]init]; ClassB *classB = [[ClassB alloc]init]; [classA performSelector:sel withObject:nil]; [classB performSelector:sel withObject:nil]; SEL selA=@selector (print:); 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:
ClassA *classA = [[ClassA alloc]init]; classA = nil id data; data=nil;