Objective-C magic path [2-objective-c programming], objectivec Programming

Source: Internet
Author: User

Objective-C magic path [2-objective-c programming], objectivec Programming

Do not repost the labor results of developers


There are two main ways to compile and run the Objective-C program:

1. Xcode integrated development environment.

2. Use the Terminal command line window of the GNU Objective-C compiler.


Use Xcode

XCODE and iOS SDK:

Pai.apple.com

Of course, you can also download it from the App Store.

Of course, you need to create a developer account, but it is not required for the time being.


The Objective-C source file ends with. m, which is the extension.

Objective-C ++ is an extension of C ++, similar to Objective-C is a extension of C.


A. Procedure for creating A new program using Xcode:

1. Start the Xcode application.

2. If you develop a New Project, select File> New Project... or Create a New Xcode Project on the start page.

3. Select the Application type and select Application-> Command Line Tool (the graphic interface is no longer required at the beginning. This is similar to the C language), and Next.

4. Get a name for the application and set the Type to Foundation. Make sure that the Use Automatic Reference Counting check box is selected (this is to Use the new feature, namely, ARC) and click Next.

5. Select the name of the project directory, select the directory in which the project file is stored, and click Create.

6. In the upper left pane, you will see the file main. m (which can be found in a folder with the same name as the project name), highlighting the file (that is, clicking the file ). Enter or edit your program in the edit window below this window.

7. In the toolbar, select the icon in the middle of the View to display the debugging area. The output result is displayed in this area. L

8. Click the Run button in the toolbar or select Run from the Product menu to compile and Run the program.

Description:






B. Procedure for writing a program using the Terminal command line:

1. Enable Terminal.

2. Select your work (Project) directory. If not:

$ Mkdir test

Go to the working directory

$ Cd test

Create and edit,

$ Vi main. m

See the following link for how to use VI:

Http://en.wikipedia.org/wiki/Vi


Enter your first Program:

// My first Program#import <Foundation/Foundation.h> int main(int argc, char *argv[]){    @autoreleasepool{        NSLog(@"Hello World!");    }  return 0;}
 

3. Compile and compile the linker using clang's LLVM Clang Objective-C. This is similar to GCC in Linux. If you don't know, you can check the GCC compilation.

$ Clang-fobjc-arc-framework Foundation main. m-o test

4. Run. Add "./" before the executable file that is compiled in the same way as Linux. You can also add the project directory to the PATH (so you can not "./"), which is generally unnecessary.

$./Test

5. The output result is displayed in the command line.

Note that Objective-C is case sensitive, that is, case sensitive. Pay attention to this when writing code.

Details:

A. Note: // or /*...*/

B. # import: Tell the compiler to locate and process the file named Foundation. H, which is a system file;
# Import: import the file information to the program;

Use the # include command in C and C ++. In this example, you can also use # include,

# Import is # include improvement command, which can prevent repeated inclusion of header files;

C, int main (int argc, const char * argv []), this method is the entry of the program.

Parameter argc-parameter number, argv-parameter value

D. @ autoreleasepool Automatically releases the pool: This enables the system to effectively manage the memory used by the application when a new object is created;

Or use the following method:

// First program example#import <Foundation/Foundation.h>int main (int argc, const char * argv[]) {NSAutoreleasePool * pool =[[NSAutoreleasePool alloc] init];// insert code here...NSLog(@"Hello World!");[pool drain];return 0;}

NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init]
A statement is used to create an automatic release memory pool object to maintain memory release and recovery,

The alloc method allocates memory, and the init method is the initialization class, which is equivalent to the C ++ or Java constructor.

The [pool drain] statement is used to release the memory pool. It releases all the object memory managed in the memory pool,

We will discuss the problem of NSAID utoreleasepool in detail in the memory management section.


E: NSLog: oc library function. It only displays or records its parameters, and can also display variables, using % I;

NSLog is the Objective-C log output function provided by the Foundation framework,
Similar to the printf function in Standard C, it can format the output.
@ "Hello, World !", Constant NSString object, which represents an NSString object. It is a string class commonly used by Objective-C.


F: retrun 0: The program Exits normally;

It indicates that the execution of main is to be terminated. Generally, 0 indicates that the execution ends normally, and non-0 indicates that the execution is abnormal,

This is the habit of using C language.


Objective-C code file extension



Objective-C20 programming is different from Objective-C Basics

Objective-c2.0 this book focuses on objective-C language, still count
The basic tutorial briefly introduces ob-c focuses on interface design, various interactions, and animation design.
We recommend that you first look at the program design. View and write interfaces.

Objective-C Programming: (The Big Bird of Objective-C will give it a hand)

MacOS...

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.