Objective-C (xcode helloworld Program Creation class)

Source: Internet
Author: User
Tags uppercase letter

I. Introduction to objective-C

Objective-C is a language for iPhone software development. Objective-C language is an extension set of C Language Objective-C is an object-oriented language. Case Sensitive The Program Statement must end (;) Development tools (xcode)

2. The first objective-C program

Start
Xcode. At the first startup, a "welcometo xcode" dialog box may pop up. We can turn it off.

Then, select "file-> New Project" in the top menu of the screen. A dialog box is displayed, indicating that you can select a project type. Select "commandlineutility" on the left of the dialog box"
Select "foundationtool" on the right and select "choose. Enter the project name "Hello World" and select Save

#import<Foundation/Foundation.h>    int main (int argc, constchar * argv[])    {        NSAutoreleasePool * pool = [[NSAutoreleasePoolalloc] init];        //----------------------------------------        NSLog(@"Hello,iphone!");                //----------------------------------------        [pool drain];        return 0;    }

# Import <Foundation/Foundation. h>

Tell the compiler to import the main header file of foundation. h In the foundation framework. Here, let's talk about cocoa:

Cocoa includes two core frameworks: foundation framework and appkit framework cocoatouch. There are two frameworks: foundation framework and uikit framework. Let's talk about the framework: the so-called framework is a collection, which contains header files, library and other resource files; each framework has a master header file that contains all header files in the framework, you can use any class in the Framework in your own program. This is the framework!
Format: int main (intargc, const char * argv [])Each program has only one main function to indicate where the program will start to run. Return 0;0: The program is normal. Non-0: It usually indicates that some problems have occurred (for example, the files required by the program may not be found) NSAID utoreleasepool * Pool = [[NSAID utoreleasepoolalloc] init];

// It can be understood that a shared memory space is opened up in the memory.

Memory management is very important in iOS development

[Pool drain];

// Release the shared memory

Nslog (@ "Hello, iPhone! ");

Nslog is a function in the objective-C library.

Purpose: output text content to the console

A constant string is displayed, similar to the printf function, and '\ n' is automatically added to the text '. You can also use escape characters. For example:

Nslog (@ "The sum of 50 and 25 is % I", sum );

//----------------------------------------

Single line comment: Use a continuous slash (//) to add multiple lines. Note: Start With/*. */The end cannot be nested. The advantage is that the program disconnected is intended to facilitate team cooperation, facilitate later code Maintenance

3. define a new class:

Select a new file in the file. In the displayed dialog box, select objective-C class. The input class is named test:

It will automatically generate the test. h and test. m Files:. H is the header file, and. m is the source file of objective-C;

The header file code of test. H is as follows:

#import <Foundation/Foundation.h>@interface Test : NSObject@end

The source code of test. m is as follows:

#import "Test.h"@implementation test@end

@ Interface

Describes the data components and methods of classes and classes.

@ Is the directive

@ Implementation

Actual code for implementing these methods

@ Interface:
@ Interface newclassname: parentclassname {

Memberdeclarations ;}

Methoddeclarations;

@ End

@ Implementation:

@ Implementation newclassname

Methoddefinitions;

@ End

It starts with @ interface and ends with @ end !!!! @ Implementation: the same is true. End with @ end !!!;

ClassNaming rules: it must start with a letter or underline, followed by any letter, underline, or 0 ~ 9Number combination. The class name starts with an uppercase letter. The instance variables, objects, and Methods names start with lowercase letters.
;
This is the creation class;

Iv. Declare and define variables and Methods

Test. h

@ Interface Test: nsobject {// The attribute must be defined in the braces int intx; int inty;} + (INT) staticinty; // belongs to the class and does not belong to the object-(void) print; // method without parameters-(void) setintx :( INT) N; // method with one parameter-(void) setinty :( INT) D;-(void) setintx :( INT) n andsetinty :( INT) D; // method of multiple input parameters-(INT) intx; // get method-(INT) inty; @ end

The sign (-) or (+) at the beginning of the function indicates:
(-) This method is an instance method (some operations are performed on a specific instance of the class); (+) is a class method (that is, a method that executes certain operations on the class itself, for example, create a new instance of the Class)

Function declaration example:

-(Void) setnumerator :( INT) n indicates the method type, return type, followed by method name, parameters accepted by the method, parameter type, parameter name

Note: If the parameter is not included, the ":" sign is not used. It can be seen from the program.
If no return type is specified, the default value is ID. All input parameters are ID by default.

Any type of object ).

Methods with multiple parameters:

-/+ (Return type) function_name: (parameter type) parameter1 otherparameter: (parameter_type) parameter2;

If there is only one parameter, the type and name of the parameter are declared at:. If there are multiple parameters, there must be one before each parameter :,
Then there is the parameter type and name. However

Test. M file

@ Implementation test + (INT) staticinty {Y + = 1; return y;}-(void) print {nslog (@ "the result of adding two numbers is: % I ", intx + inty);}-(void) setintx :( INT) n {intx = N;}-(void) setinty :( INT) d {inty = D;}-(void) setintx :( INT) n andsetinty :( INT) d {intx = N; inty = D;}-(INT) intx {return intx;}-(INT) inty {return inty ;} @ end

There is nothing to say about this implementation class. I believe everyone can understand it, just the implementation method;

Then we tested it in the main. M file:

Test * test = [[test alloc] init]; // object declaration and initialization. alloc applies for memory space for the object, and init completes object initialization. After object declaration and initialization, you can call the object method. [Test setintx: 1]; // call a method [test setinty: 1]; [test print]; // call a method without a parameter [test setintx: 2 andsetinty: 2]; // call the [test print] method with multiple parameters.

Create a test object:

Test * test = [[test alloc] init];

Be sure to have an asterisk on the right of the data type. All objective-C object variables are pointer-type. The statement on the right of the equal sign is to create an object, which is a nested method call. The first method called is the alloc method of test. This is a relatively low-level call, because the method actually applies for a content space for the test variable, and the second calls the init method of the newly created object, this init method is used to initialize the variable value. Init implements Common settings, such as setting the initial values of instance variables.

The following two statements are called:

[Object Name Method Name: real parameter]; a parameter is called. If there is no parameter, there is no colon and the following. If there is a multi-parameter: [Object Name Method Name: Real Parameter Method Name: parameters ].

Note: To create a new object, you must request memory allocation. When you complete operations on the object, you must release the memory space used by the object.

The I Phone platform does not support the garbage collection mechanism. To access instance variables externally, you must use the class method to retrieve the value.

In objective-C, all objects can be distinguished by IDs. We know that a class is only the code for adding operations on the data.
So ID is actually a pointer to the data structure, which is equivalent to void *.

To sum up the object declaration Syntax:

Class name * test, * test1;

Test and test1 are defined as the objects of the specified class. Note that this only defines a pointer variable and does not obtain memory space for the data it contains. After the alloc method is called, these objects obtain (allocate) space.

Output result:

11:24:00. 522 helloworld [652: 707]Add two numbersResultIs:2

11:24:00. 612 helloworld [652: 707]Add two numbersResultIs:4

Reprinted, please specify the original location !!!

Reprinted, please specify the original location !!!
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.