"Objective-c" 04-First OC Program parsing

Source: Internet
Author: User

Description: This objective-c topic is the prelude to learning iOS development. Also in order to have the object-oriented language development experience of the program ape, can be high-speed 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 session, the first OC program has been created, which is to parse its internal code.

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

Open the main.m file with code such as the following

1 #import<Foundation/Foundation.h>2 3 intMainintargcConst Char*argv[])4 {5 6 @autoreleasepool {7         8         //Insert code here ...9NSLog (@"Hello, world!.");Ten          One     } A     return 0; -}
1. Entry point of the program: main function

Just like 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 in which the system proactively reclaims objects that are no longer in use, and OC does not support garbage collection. Developers are required to write code to release the memory occupied by the object. In line 6th there is a @autoreleasepool{}, which is related to memory management, temporarily do not understand its meaning, you just need 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 incoming OC string parameters to the console.

2> function References @ "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 NSLog's own initiative, such as the log output time, the project name and so on. and NSLog output after the completion of their own initiative line .

4> NSLog can also support the format character

like the C language printf function

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

*%i means receiving integer data

*%f indicates receiving floating-point data. %.2f indicates 2 decimal places reserved

* Output Result:

 --Geneva- .  -: +:07.380The first OC program [693:303] My Age is Tenand height is 1.55

4. #import

1> #import是一个预处理指令, similar to the C language, includes (copies) the contents of a file to the location of the preprocessing Directives .

The #import <Foundation/Foundation.h> representation in line 1th of 2> includes the Foundation.h file in the Foundation framework.

1) The foundation framework includes very many commonly used classes and functions, such as the string processing class NSString, the 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 language topic:. h is called a header file and is typically used to declare some functions that you want to use. 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 first to include the Foundation.h file talent using the NSLog function. It's like in Java, when you introduce a class with Importkeyword, you have the ability to use this class normally.

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

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

In OC, we use #import to include header files, and the advantage is the ability to proactively prevent the same header files from being included multiple times .

5> #import <...> represents the files that are included with the system. #import "..." means including files created by developers themselves

Category: Non-0 Basic learning iOS development 2-objective-c

"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.