OBJECTIVE-C] Getting Started (Xcode HelloWorld program Creation Class

Source: Internet
Author: User
Tags uppercase letter

A: objective-c introduction

Objective-c is the language of iphone software development Objective-c language is an extended set of C languages Objective-c is an object-oriented language Case Sensitive The program statement must be (;) end Development Tools (Xcode)

Second, the first objective-c procedure

Start Xcode. The first time you start, you might pop up a dialog box for "Welcometo Xcode," and we can turn it off.

Then you select "File->new Project" on the upper menu of the screen, and a dialog box appears allowing you to select the type of project. You need to select "Commandlineutility" on the left side of the dialog box, then select "Foundationtool" on the right and select the "Choose ..." button. Enter the project name "Hello World" and select Save As

---------------------------------------------------------------------
  1. #import <Foundation/Foundation.h>
  2. int main (int argc, Constchar * argv[])
  3. {
  4. NSAutoreleasePool * Pool = [[Nsautoreleasepoolalloc] init];
  5. //----------------------------------------
  6. NSLog (@ "hello,iphone!");
  7. //----------------------------------------
  8. [Pool drain];
  9. return 0;
  10. }

---------------------------------------------------------------------

#import <Foundation/Foundation.h>

Tells the compiler to import the Foundation.h master header file in the foundation framework; In this case, let's say Cocoa:cocoa contains two core frameworks: the foundation framework and the AppKit framework Cocoatouch contain two frameworks: Foundation Framework and Uik It framework again framework: the so-called framework, is a set, which contains the header files, libraries and other resource files, each frame has a master header file, the header file contains the framework of each header file, but the introduction of the main header file, you can use in their own programs in the framework of any class; This is the framework! format: int main (INTARGC, const char* argv[])Each program has and only one main function indicates where the program will start executing. return 0;0: Program normal non 0: usually indicates some problems (such as: it is possible to find the files required by the program) NSAutoreleasePool *pool = [[Nsautoreleasepoolalloc] init];

Can be understood to open up a piece of shared memory space in memory

Memory management is very important in iOS development

[Pool drain];

Free up shared memory space

NSLog (@ "hello,iphone!");

NSLog is a function in the OBJECTIVE-C library

Role: Output text content to the console

A constant string is displayed, similar to the printf function, and it automatically adds ' n ' after the text. Of course, you can also use escape characters. For example, there are:

NSLog (@ "The sum of the is%i", sum);

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

Single-line Comment: Use consecutive slashes (//) multiline comments: Use end not nested use benefits it is clear that the program is intended to facilitate teamwork and facilitate later code maintenance


Third, define a new class:

Select New file in file, in the Open dialog box, select Objective-c class, and the input class name is test:

It will automatically generate the TEST.H,TEST.M file:. h is the header file,. m is the source file for objective-c;

The first is the Test.h header file code as follows:

---------------------------------------------------------------------
    1. #import <Foundation/Foundation.h>
    2. @interface Test:nsobject
    3. @end

---------------------------------------------------------------------

Then the TEST.M source file code is as follows:

---------------------------------------------------------------------
    1. #import "Test.h"
    2. @implementation Test
    3. @end

---------------------------------------------------------------------

@interface section

Methods for describing the data components of classes, classes, and classes

@ is the instruction symbol

@implementation section

The actual code that implements these methods

General format for @interface section:
@interface newclassname:parentclassname{

Memberdeclarations;}

Methoddeclarations;

@end

General format for @implementation section:

@implementation Newclassname

Methoddefinitions;

@end

It starts with @interface and ends with @end!!!! @implementation the same, ending the!!! with @end;

naming rules for classes: start with a letter or underscore, and then a combination of any letter, underscore, or 0~9 number, with the class name beginning with an uppercase letter, and the name of the instance variable, object, and method starting with a lowercase letter;
This is the creation of classes;

IV. declaration of defined variables and methods

Test.h

---------------------------------------------------------------------
    1. @interface test:nsobject{//attribute must be defined in this curly brace
    2. int IntX;
    3. int inty;
    4. }
    5. + (int) staticinty;//belongs to class, not to Object
    6. -(void) print;//non-parametric method
    7. -(void) Setintx: (int) n;//method with one parameter
    8. -(void) Setinty: (int) D;
    9. -(void) Setintx: (int) n andsetinty: (int) method for d;//multiple input parameters
    10. -(int) Intx;//get method
    11. -(int) inty;
    12. @end

---------------------------------------------------------------------

The (-) or (+) numbers at the beginning of the function indicate:
(-) The method is an instance method (performing some action on a particular instance of a class);(+) is a class method (that is, a method that performs some action on the class itself, such as creating a new instance of a class)

Examples of declarations of functions:

-(void) Setnumerator: (int) n The first representation of a method type, a return type, followed by a method name, a method-accepted parameter, a parameter type, an argument name

Note: If you do not use the ":" Number without parameters, you can see from the program
If no return type is specified, the default is the ID type, and all input parameters are the ID type by default (the ID type can be used to reference

Objects of any type).

Methods that have multiple parameters:

-/+ (return type) Function_name: (parameter type) Parameter1 Otherparameter: (parameter_type) parameter2;

If there is only one parameter, declare the type and name of the parameter after:, and if there are multiple arguments, precede each argument with one: and then the parameter type and the parameter name. But

TEST.M file ---------------------------------------------------------------------
  1. @implementation Test
  2. + (int) staticinty{
  3. Y+=1;
  4. return Y;
  5. }
  6. -(void) print{
  7. NSLog (@ "Two numbers added results:%i", intx+inty);
  8. }
  9. -(void) Setintx: (int) n{
  10. Intx=n;
  11. }
  12. -(void) Setinty: (int) d{
  13. Inty=d;
  14. }
  15. -(void) Setintx: (int) n andsetinty: (int) d{
  16. Intx=n;
  17. Inty=d;
  18. }
  19. -(int) intx{
  20. return IntX;
  21. }
  22. -(int) inty{
  23. return inty;
  24. }
  25. @end
---------------------------------------------------------------------
This implementation class has nothing to say, I believe we can understand, just to achieve the method;

Then it was tested in the MAIN.M file:

---------------------------------------------------------------------
    1. Test *test=[[test the Declaration and initialization of the alloc]init];//object, where Alloc requested memory space for the object, and Init completed the initialization of the object. After the object declaration and initialization has been completed, the method of the object can be called.
    2. [Test setintx:1];//method call
    3. [Test setinty:1];
    4. [Test print];//no parameter call
    5. [Test Setintx:2 andsetinty:2];//Multi-argument method call
    6. [Test print];

---------------------------------------------------------------------
Create a Test object:

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

It is important to note that there is an asterisk to the right of the data type. All Objective-c object variables are pointer-type. The statement to the right of the equal sign is the creation of an object, which is a nested method call. The first call is the Alloc method of test. This is a relatively low-level call, because the method actually requests a content space for the test variable, and the second invokes the Init method of the newly created object, which is used to initialize the value of the variable. Init implements more commonly used settings, such as setting the initial value of an instance variable.

The following two sentences are called methods:

"Object Name Method Name: argument"; if there is no argument, there is no colon and later, if it is multi-parameter: "Object Name Method Name: Argument method Name: Argument".

Note : Creating a new object requires allocating memory, and when you complete the operation on that object, you must free the memory space it uses

I phone platform does not support garbage collection mechanism external to access an instance variable requires the method of the class to retrieve its value, not directly accessing

In Objective-c, all objects can be distinguished using IDs. We know that a class is just some data plus code that operates on that data, so the ID is actually a pointer to a data structure, equivalent to void*.

Summarize the syntax of the object declaration:

Class name *test,*test1;

Defines which test and test1 are objects of the specified class. Note that this simply defines a pointer variable that has not yet obtained memory space for the data it contains. These objects receive (allocate) space after the Alloc method is called.

The output is:

2012-03-24 11:24:00.522 helloworld[652:707] two numbers added the results are: 2

2012-03-24 11:24:00.612 helloworld[652:707] two numbers added the results are: 4

OBJECTIVE-C] Getting Started (Xcode HelloWorld program Creation Class

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.