[Objective-C] 04-parsing the first OC Program

Source: Internet
Author: User

Note: This Objective-C topic is a prelude to iOS development. It also helps programmers with experience in object-oriented language development to quickly get started with Objective-C. If you have no programming experience or are not interested in Objective-C or iOS development, ignore this. Before learning this topic, we recommend that you first study the C language topic.

In the previous section, the first OC program has been created, which resolves its internal code.

Check the project structure and find that there is only one source file in this program: main. m

Open the main. m file. The Code is as follows:

 1 #import <Foundation/Foundation.h> 2  3 int main(int argc, const char * argv[]) 4 { 5  6     @autoreleasepool { 7          8         // insert code here... 9         NSLog(@"Hello, World!");10         11     }12     return 0;13 }
1. entry point of the program: main Function

Like the C program, the entry point of the OC program is still the main function. A main function has been defined in row 3rd of main. m.

 

2. @ autoreleasepool

In Java, there is a garbage collection mechanism, and the system will automatically recycle objects that are no longer in use. OC does not support garbage collection. developers need to write code to release the memory occupied by objects. There is a @ autoreleasepool {} in row 6th, which is related to memory management and does not need to be understood for the time being. You just need to remember: the later OC code will be written inside {} of @ autoreleasepool.

 

3. NSLog

1> A 9th-row NSLog is a log output function that can output the passed OC string parameters to the console.

2> function parameter @ "Hello, Wolrd! "Is an OC string, not a C language string. All OC strings are preceded @.

3> 9th lines of code NSLog (@ "Hello, World! "); The output result is:

The information on the left side of the red box is automatically added to NSLog, such as the log output time and project name. In addition, NSLog is automatically wrapped after output.

4> NSLog supports format operators like the printf function in C language.

1 int age = 10;2 NSLog(@"My age is %i and height is %.2f", age, 1.55f);

* % I indicates receiving Integer Data

* % F indicates receiving floating point data, and %. 2f indicates retaining 2 decimal places

* Output result:

17:43:07. 380 first OC program [693: 303] My age is 10 and height is 1.55

 

4. # import

1> # import is a pre-processing command. Its function is similar to # include in C language. It contains (copies) the content of a file to the position of the pre-processing command.

2> the # import <Foundation/Foundation. h> in line 1 contains the Foundation. h file in the Foundation framework.

1) The Foundation framework contains many common classes and functions, such as string processing class NSString and log output function NSLog. It is equivalent to Java. lang. * in java .*

2) location of the Foundation framework and Foundation. h file:

3> I said in the third lecture on C language:. h is called a header file. It is generally used to declare some functions. To use these functions, you must use # include to include the header file of the function. The NSLog function we used in row 9th exists in the Foundation. h file. Therefore, you must first include the Foundation. h file before using the NSLog function. It seems that in Java, you can use this class only after introducing a class with the import keyword.

4> in C \ C ++, we use # include to include the header file. The disadvantage is that the same header file may be contained multiple times. To solve this problem, the header file is usually written as follows:

#ifndef _TEST_H_#define _TEST_H_/*.....*/#endif

In OC, we use # import to include the header file. The advantage is that it can automatically prevent the same header file from being contained multiple times.

5> # import <...> indicates that the file that comes with the system is included. # import "..." indicates that the file created by the developer is included.

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.