[Objc] Objective-C Learning (1)

Source: Internet
Author: User

I. Development Environment

If you use xcode, you can download it from the App Store. However, the download speed is too slow. You have downloaded and installed the command line tool on your own. I do not know whether the command line tool is repeated.

The version 4.4.1 is used. Many tutorials are of the old version, so some of them are not the same.

Create a command line program and use objc:

Create a project-> Mac --- application --- command line tool ---> select Foundation .. The command line program of objc is used.

 

Ii. Basic syntax

1. Use # import to ensure that the header file is referenced only once, saving the trouble of # ifdef and # endif.

2. Foundation is a framework. A framework is a collection of components in a unit, including header files, libraries, images, and sound files. Cocoa consists of the Foundation and application KIT framework.

3. Cocoa adds the "ns" prefix to all functions, constants, and type names.

Nslog (@ "Hello World ");

@ Indicates that the string is processed as an nsstring.

4. bool type: with yes and no values,

Different from the bool type of C, the non-zero bool value is not necessarily No.

 

3. Object-oriented

1. Void drawshape (ID shapes [], int count)

{

Int I;

For (I = 0; I <count; I ++ ){

Id shape = shapes [I];

[Shape draw];

}

}

ID: a generic object used to represent any class.

[Shape draw] can be understood as sending a draw message to a shape object, or calling the draw method for a shape object.

 

2. Define the interface @ Interface

@ Interface circle: nsoject // inheritance

{

Shapecolor fillcolor;

Shaperect bounds;

}

-(Void) setfillcolor: (shapecolor) fillcolor; // The type is enclosed by ().-The short horizontal representation of the declared method rather than the Function

-(Void) draw;

@ End // circle

 

Method call with two parameters

[Textthing setstringvalue: @ "hell0"

Color: kbluecolor];

 

3. Interface/method implementation @ implementation

4. Object Instantiation [circle new];

Id shape = [circle new];

 

5. Inheritance, single inheritance, self keyword pointing to the object itself, super keyword referencing parent class members

6. Composite: composite is implemented by containing object pointers as instance variables. (Combination of objects)

@ Interface unicycle: nsobject

{

Tire * tire;

}

7. access method. Do not use get as the prefix of the getter Method

 

Iv. header file. H. Implement question. m

1. Circular dependency, @ class is very useful

That is, Class A uses Class B, and Class B also uses Class A. If the # Import Statement is attempted to make the two classes reference each other, a compilation error occurs.

You can use @ Class B in A. H and @ Class A in B. H.

@ Class command can reduce Compilation Time, it tells the compiler "Believe me, you can finally understand the class of this name"

 

5. Use xcode and shortcut keys

Command [move code block to the left

Tab accept code prompt

ESC display code prompt menu

Control-. Circular browsing code prompt

Control-F forward cursor

Control-B move back

Control-P move up

Control-n move down

First Line of Control-

End of control-e

Control-D Delete the character on the right of the cursor

Control -- k delete a row

Command-alt-P continue (debugging)

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.