Basic Data Types of Objective-C syntax

Source: Internet
Author: User

This blog introduces the length of the basic data type, how to use the basic data type in the array of Oc, and the conversion of other data types.

To demonstrate the basic data type example, we create an iPhone project class as an example.

1. Create a project

For convenience, we create a Single View Application.

Enter the project name BaseType

Product Name: the Product Name, similar to the project Name.
Company Identifier: Company Identifier. Generally, the naming rule is "com. Company Name"
Bundle Identifier: a package Identifier used to uniquely identify an application. It is generated by combining the company Identifier and product name by default.
Device Family: The Device type supported by the app. There are three options: iPhone, iPad, and Universal (for iPhone and iPad)
Include Unite Tests: whether to Include the unit test code template. If this option is selected, Xcode will help generate the unit test code template.

Find in the project, ViewController. m to facilitate the demonstration, we add the test code when starting the interface.

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,

Running result:

2012-06-13 13:55:46.726 BaseType[3032:f803] The size of an int is: 4 bytes.2012-06-13 13:55:46.726 BaseType[3032:f803] The size of a short int is: 2 bytes.2012-06-13 13:55:46.727 BaseType[3032:f803] The size of a long int is: 4 bytes.2012-06-13 13:55:46.731 BaseType[3032:f803] The size of a char is: 1 bytes.2012-06-13 13:55:46.732 BaseType[3032:f803] The size of a float is: 4 bytes.2012-06-13 13:55:46.733 BaseType[3032:f803] The size of a double is: 8 bytes.2012-06-13 13:55:46.733 BaseType[3032:f803] The size of a bool is: 1 bytes.

3. Format output data

// Integer int integerType = 5; // float floatType = 3.1415; // double doubleType = 2.2033; // short integer short int Integer type = 200; // long int longlongType = 7758123456767L; // C string char * cstring = "this is a string! "; // Integer NSLog (@" The value of integerType = % d ", integerType); // floating point 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", longlongType ); // C string NSLog (@ "The value of cstring = % s", cstring );

Result:

2012-06-13 14:06:18.757 BaseType[3215:f803] The value of integerType = 52012-06-13 14:06:18.757 BaseType[3215:f803] The value of floatType = 3.142012-06-13 14:06:18.758 BaseType[3215:f803] The value of doubleType = 2.203300e+002012-06-13 14:06:18.758 BaseType[3215:f803] The value of shortType = 2002012-06-13 14:06:18.758 BaseType[3215:f803] The value of longlongType = 77581234567672012-06-13 14:06:18.758 BaseType[3215:f803] The value of cstring = this is 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 in this way.

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.