"Objective-c" 04-First OC Program parsing

Source: Internet
Author: User

Description: This objective-c topic is a prelude to learning iOS development, and to enable programmers with experience in object-oriented language development to quickly get started with objective-c. If you do not have programming experience, or are not interested in objective-c, iOS development, please ignore. Before studying this topic, it is recommended to study the C language topic first.

In the previous speech, the first OC program has been created, which is to parse its internal code.

View project structure you can find only one source file in this program: MAIN.M

Open the Main.m file with the following code

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!"); Ten     }12     return 0;13}
1. Entry point of the program: main function

As with the C program, the entry point of the OC program is still the main function. A main function has been defined on line 3rd of MAIN.M.

[email protected]

In Java, there is a garbage collection mechanism that automatically reclaims objects that are no longer in use, and OC does not support garbage collection, requiring developers to write code to free up memory used by objects. In line 6th there is a @autoreleasepool{}, which is related to memory management, for the time being do not understand its meaning, you just have to remember: The Future OC Code is written in the @autoreleasepool {} inside.

3.NSLog

1> Line 9th, NSLog, is a log output function that outputs the passed-in OC string parameter to the console.

2> function parameter @ "Hello, wolrd!" is an OC string, not a C language string, and all OC strings have a @ at the front.

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

The message to the left of the red box is automatically added to the NSLog, such as the log output time, the project name, and so on. And the NSLog output is finished and then wrapped automatically.

4> NSLog can also support format characters like the C-language printf function

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

*%i means receiving integer data

*%f means to receive floating-point data,%.2f means to retain 2 decimal places

* Output Result:

2013-04-06 17:43:07.380 First OC program [693:303] My age is ten and height is 1.55

4. #import

The 1> #import是一个预处理指令, similar to the C # # include, contains (copies) the contents of a file to the location of the preprocessing directives.

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

1) The foundation framework contains many commonly used classes and functions, such as String processing class NSString, log output function NSLog. Its importance is equivalent to the java.lang.* in Java

2) Location of foundation framework and Foundation.h files:

3> I said in the third of the C language topic:. h is called a header file and is generally used to declare functions that you want to use, and you must include the header file that contains the function. The NSLog function we used in line 9th exists in the Foundation.h file, so it is necessary to include the Foundation.h file before you can use the NSLog function. It's like in Java, when you introduce a class with the Import keyword, you can use the class normally.

4> in c\c++, we include a header file with # include, the disadvantage is that the same header file may be included multiple times. In order to solve this problem, it is common to write the header file:

#ifndef _test_h_#define _test_h_/*.....*/#endif

In OC, we use #import to include header files, and the advantage is that it automatically prevents the same header file from being included multiple times.

5> #import <...> represents a file containing the system's own, #import "..." means containing the files created by the developer themselves

"Objective-c" 04-First OC Program parsing

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.