You have encountered Objective-c's basic data type int. Recall that a variable declared to be of type int can only be used to hold an integer value, that is, a value that does not contain a decimal place.
OBJECTIVE-C also provides 3 additional basic data types: float, double, and Char. Variables declared as float can store floating-point numbers (that is, values that contain decimal digits). The double type, like the float type, typically represents about twice times the range of the latter. The char data type can store a single character, such as the letter A, the numeric character 6, or a semicolon (which will be discussed in more detail later).
In Objective-c, any number, single character, or string is often called a constant. For example, the number 58 represents a constant integer value with the string @ "programming in Objective-c are fun." Represents a constant string object. An expression that consists entirely of constant values is called a constant expression. Therefore, the following expression is a constant expression, because each item of the expression is a constant value:
128 + 7-17
However, if I is declared as an integer variable, then the expression is not a constant expression:
128 + 7–i int type
An integer constant consists of a sequence of one or more numbers. The minus sign before the sequence indicates that the value is a negative number. Values 158,-10, and 0 are all valid integer constants. Spaces are not allowed in the middle of a number, and commas cannot be used (therefore, 12,000 is an illegal integer constant, which must be written as 12000).
Each value, whether it is a character, an integer, or a floating-point number, has its corresponding range. This range is related to the amount of memory allocated by the system for a particular type of value. In general, this amount is not specified in the language, and it is usually dependent on the computer being run, so it is called a device or machine-related amount. For example, an integer can occupy 32 bits on a computer, or you can use 64-bit storage. If you use 64-bit storage, integer variables will be able to store values that are larger than 32 bits.
Attention
In Mac OS x, the option to choose whether the application is compiled under 32-bit or 64-bit is provided. In the former case, an int occupies 32 bits, and in the latter case an int occupies 64 bits. Float Type
Variables declared as float type can store values that contain decimal digits. To differentiate a floating-point constant, you can see if it contains a decimal point. A value of 3., 125.8, and-.0001 are all valid floating-point constants. To display floating-point values, use NSLog to convert characters%f or%g.
Floating-point constants can also be represented by the so-called scientific notation. A value of 1.7e4 is a floating-point value represented by this notation, which represents the value 1.7x104.
As mentioned above, the double type is very similar to the float type, except that a variable of type double can be stored about twice times the range of the float variable. Char type
Char variables can store a single character. You can get character constants by putting characters in a pair of single quotes. Therefore, ' a ', '; ' and ' 0 ' are both valid character constants. The first constant represents the letter A, the second represents a semicolon, and the third represents the character 0, which is not the same as the number 0. Do not confuse character constants with C-style strings, character constants are single characters placed in single quotes, and strings are any number of characters placed in double quotation marks. As mentioned in the 3rd chapter, the string preceded by the @ character and placed in double quotation marks is the NSString string object.
The character constant ' \ n ' (that is, a newline character) is a valid character constant, although it seems to contradict the rules mentioned earlier. This is because the backslash symbol is considered to be a special symbol. In other words, the Objective-c compiler considers the character ' \ n ' as a single character, although it actually consists of two characters. Other special characters also start with a backslash character. You can use the format character%c in the NSLog call to display the value of a char variable.
In code listing 4-1, the basic objective-c data type is used.
Code Listing 4-1
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
@autoreleasepool {
int integervar = 100;
float Floatingvar = 331.79;
Double Doublevar = 8.44e+11;
Char Charvar = ' W ';
NSLog (@ "Integervar =%i", Integervar);
NSLog (@ "Floatingvar =%f", Floatingvar);
NSLog (@ "Doublevar =%e", Doublevar);
NSLog (@ "Doublevar =%g", Doublevar);
NSLog (@ "Charvar =%c", Charvar);
}
return 0;
}
Code Listing 4-1 Output
Integervar = 100
Floatingvar = 331.790009
Doublevar = 8.440000e+11
Doublevar = 8.44e+11
Charvar = W
In the second line of the program output, you'll notice that the value assigned to Floatingvar is 331.79, which actually shows up as 331.790009. In fact, the actual displayed value is determined by the specific computer system used. This inaccurate value occurs because the computer uses a special way to represent a number. When you use a calculator to process numbers, you are likely to experience the same inaccuracy. If the calculator calculates 1 divided by 3, the result will be obtained. 33333333, it is possible to end with some additional 3. This string of 3 is the approximate value calculated by the calculator 1/3. Theoretically, there should be an infinite 3. However, the calculator only holds the digits of these digits, which is the uncertainty of the computer. The same uncertainty arises here: some floating-point values cannot be accurately represented in computer memory. qualifier: Long, long long, short, unsigned, and signed
If the qualifier long is placed directly before the Int declaration, the declared integer variable has an extended range of values on some computers. An example of a long int declaration is:
long int factorial;
This statement declares the variable fractorial as an integer variable of type long. Just like the float and double variables, the specific range of the long variable is also determined by the specific computer system.
To display the value of long int with nslog, use the letter L as the modifier before the integer format symbol. This means that the format symbol%li will display the value of long int in decimal format.
You can also use a long long int variable, even a long double with a larger range with a floating-point number.
When you place a qualifier short before an int declaration, it tells the Objective-c compiler to declare a specific variable to store a fairly small integer. The short variable is used primarily because of memory-saving considerations, and short variables can be used to solve this problem when the programmer needs a lot of memory and the amount of memory available is very limited.
The last qualifier that can be placed before an int variable is used when an integer variable is used only to store positive numbers. The following statement
unsigned int counter;
Declares to the compiler that the variable counter is used only to hold positive values. You can extend the range of integer variables by restricting the use of integer variables so that they are specifically used to store positive integers. ID Type
The ID data type can store objects of any type. In a sense, it is a generic object type. For example, Cheng
ID graphicobject;
Declare graphicobject as a variable of type ID. The method can be declared with the return value of the ID type, as follows:
-(ID) newObject: (int) type;
This program line declares an instance method named NewObject with a single integer parameter named type and a return value of type ID.
The ID type is an important data type that is often used in this book. The purpose of this type is to maintain the integrity of the book. The ID type is an important feature in Objective-c, which is the basis for polymorphism and dynamic binding, which are discussed in detail in the 9th chapter, "polymorphic, dynamic type, and dynamic binding."
Table 4.1 summarizes the basic data types and qualifiers.
Table 4.1 Basic data types
|
Type |
Instance |
NSLog characters |
|
Char |
' A ', ' \ n ' |
%c |
|
Short int |
— |
%hi,%HX,%ho |
|
unsigned short int |
— |
%hu,%hx,%ho%hu,%HX,%ho |
|
Int |
12,-97, 0XFFE0, 0177 |
%i,%x,%o |
|
unsigned int |
12u, 100U, 0XFFu |
%u,%x,%o |
|
Long int |
12L,-2001, 0xffffL |
%li,%LX,%lo |
|
unsigned long int |
12UL, 100ul, 0xffeeUL |
%lu,%LX,%lo |
|
Long Long int |
0XE5E5E5E5LL, 500LL |
%lli,%LLX, &llo |
|
unsigned long long int |
12ull, 0xffeeULL |
%llu,%LLX,%llo |
|
Float |
12.34f, 3.1e-5f, 0X1.5P10, 0x1p-1 |
%f,%e,%g,%a |
|
Double |
12.34, 3.1e-5, 0X.1P3 |
%f,%e,%g,%a |
|
Long double |
12.34L, 3.1e-5l |
%LF, $Le,%LG |
|
Id |
Nil |
%p |
Attention
In table 4.1, starting with 0 in an integer constant means that the constant is octal (base 8), starts with 0x or (0X) indicates that it is hexadecimal (radix 16), and the numeric 0x.1p3 represents a hexadecimal floating-point constant. Don't worry about these formats, this is just a summary of the complete table. In addition, prefixes F, L (L), U (U), and LL (LL) are used to explicitly indicate that constants are of type float, long, unsigned, and long long.
This article is excerpted from the OBJECTIVE-C Program Design (4th edition)
Publishing Industry Publishing House
Beauty Stephen G. Kochan (Stephen F. G. Kochan)
Lin Yu noritoshi Zhu Yixin Translation