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]
- 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:
[Cpp]
- Id data;
- Data = person;
- [Data print];
- ...
- Data = person1;
- [Data put];
- ...
Iii. BOOL
In objc. h, BOOL is defined:
[Cpp]
- 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:
[Cpp]
- 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:
[Cpp]
- SEL sel = @ selector (print );
- ClassA * classA = [[ClassA alloc] init];
- ClassB * classB = [[ClassB alloc] init];
- [ClassA initialize mselector: sel withObject: nil];
- [ClassB extends mselector: 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:
[Cpp]
- ClassA * classA = [[ClassA alloc] init];
- ClassA = nil
-
- Id data;
- Data = nil;