1. Summary:
1) int --> nsnumber: numberwithint
2) nsnumber --> nsinteger: integervalue
3) string --> double: initwithstring
4) cgfloat --> dobule: initwithfloat, decimalobj doublevalue
5) Use nsinteger, because you do not need to consider whether the device is 32-bit or 64-bit.
6) nsinteger is a basic type, but nsnumber is a class. If you want to store a value in nsmutablearray, you cannot directly use nsinteger, for example, in an nsmutablearray.
7) nsstring and nsinteger Conversion
Nsstring * string = [nsstring stringwithformat: @ "% d", integernumber];
Integer = [String intvalue];
Static void numbertest (){
Nsnumber * numobj = [nsnumber numberwithint: 2];
Nslog (@ "numobj = % @", numobj );
Nsinteger myinteger = [numobj integervalue];
Nslog (@ "myinteger = % d", myinteger );
Int A = [numobj intvalue];
Nslog (@ "A = % d", );
// Use the cgfloat and nsdecimalnumber objects to process floating point values:
Nsdecimalnumber * mydecimalobj = [[nsdecimalnumber alloc] initwithstring: @ "23.30"];
Nslog (@ "mydecimalobj doublevalue = % 6.3f", [mydecimalobj doublevalue]);
Cgfloat mycgfloatvalues = 43.4;
Nsdecimalnumber * myotherdecimalobj = [[nsdecimalnumber alloc] initwithfloat: mycgfloatvalue];
Nslog (@ "mytherdecimalobj doublevalue = % 6.5f", [mytherdecimalobj doublevalue]);
}
2. Length of basic data types in C Language
- Nslog (@ "the size of an int is: % lu bytes .",Sizeof(Int));
- Nslog (@ "the size of a short Int Is: % lu bytes .",Sizeof(Short Int));
- Nslog (@ "the size of a long Int Is: % lu bytes .",Sizeof(Long Int));
- Nslog (@ "the size of a char is: % lu bytes .",Sizeof(Char));
- Nslog (@ "the size of a float is: % lu bytes .",Sizeof(Float));
- Nslog (@ "the size of a double is: % lu bytes .",Sizeof(Double));
- Nslog (@ "the size of a bool is: % lu bytes .",Sizeof(Bool); // Do any additional setup after loading the view,
Result:
- 13:55:46. 726 basetype [3032: f803] the size ofIntIs: 4 bytes.
- 13:55:46. 726 basetype [3032: f803] the size ofShort IntIs: 2 bytes.
- 13:55:46. 727 basetype [3032: f803] the size ofLong IntIs: 4 bytes.
- 13:55:46. 731 basetype [3032: f803] the size ofCharIs: 1 bytes.
- 13:55:46. 732 basetype [3032: f803] the size ofFloatIs: 4 bytes.
- 13:55:46. 733 basetype [3032: f803] the size ofDoubleIs: 8 bytes.
- 13:55:46. 733 basetype [3032: f803] the size ofBoolIs: 1 bytes.
3. Format output data
- // Integer
- IntIntegertype = 5;
- // Floating point type
- FloatFloattypes = 3.1415;
- // Double Floating Point
- DoubleDoubletype = 2.2033;
- // Short integer
- Short IntStruct type = 200;
- // Long integer
- Long Long IntLonglongtype = 7758123456767l;
- // C language string
- Char* Cstring = "this is a string! ";
- // Integer
- Nslog (@ "the value of integertype = % d", integertype );
- // Floating point type
- Nslog (@ "the value of floattype = %. 2f", floattype );
- // Double Floating Point
- Nslog (@ "the value of doubletype = % E", doubletype );
- // Short integer
- Nslog (@ "the value of parameter type = % Hi", parameter type );
- // Long integer
- Nslog (@ "the value of longlongtype = % llI", longlonglongtype );
- // C language string
- Nslog (@ "the value of cstring = % s", cstring );
Result:
- 14:06:18. 757 basetype [3215: f803] the value of integertype = 5
- 14:06:18. 757 basetype [3215: f803] the value of floattype = 3.14
- 14:06:18. 758 basetype [3215: f803] the value of doubletype = 2.203300e + 00
- 14:06:18. 758 basetype [3215: f803] the value of Role type = 200
- 14:06:18. 758 basetype [3215: f803] the value of longlongtype = 7758123456767
- 14:06:18. 758 basetype [3215: f803] the value of cstring =ThisIs a string!
4. Int, nsinteger, nsuinteger, nsnumber
1. when int type variables are required, you can use Int or nsinteger, just like a program that writes C. But nsinteger is recommended, in this case, you do not need to consider whether the device is 32-bit or 64-bit.
2. nsuinteger is unsigned, that is, there is no negative number, and nsinteger is signed.
3. Some people say that since nsinteger and other basic types are available, why does nsnumber exist? Of course, their functions are different.
Nsinteger is a basic type, but nsnumber is a class. If you want to store a value in nsmutablearray, you cannot directly use nsinteger, for example, in an nsmutablearray:
- Nsmutablearray * array = [[nsmutablearray alloc] init];
- [Array addobject: [nsnumber numberwithint: 88];
This will cause a compilation error, because nsmutablearray requires a class, but '88' is not a class.
Cocoa provides the nsnumber class for packaging (that is, implemented in the form of objects) Basic data types.
For example:
+ (Nsnumber *) numberwithchar: (char) value;
+ (Nsnumber *) numberwithint: (INT) value;
+ (Nsnumber *) numberwithfloat: (float) value;
+ (Nsnumber *) numberwithbool: (bool) value;
After encapsulating the basic data type into nsnumber, you can obtain it again using the following instance method:
-(Char) charvalue;
-(INT) intvalue;
-(Float) floatvalue;
-(Bool) boolvalue;
-(Nsstring *) stringvalue;
Example:
- Nsnumber * num = [nsnumber numberwithint: 88];
- Nsinteger integer = [num intvalue];
5. nsstring and nsinteger Conversion
- Nsinteger integernumber = 888;
- Nsstring * string = [nsstring stringwithformat: @ "% d", integernumber];
- Nslog (@ "string is % @", string );
- Integer = [String intvalue];
- Nslog (@ "integer is % d", integernumber );
Char float and other types can be converted.