The preliminary experience of the Dark Horse programmer--objective-c's--oc

Source: Internet
Author: User

I. INTRODUCTION of OC

(i), the origins of Objective-c and C

Born in the 1980s, Objective-c was invented by Brad Cox to combine the popular, portable C language with the elegant Smalltalk language.

Objective-c is an extended set of C languages that, based on the C language, adds subtle but significant features to the language.

Apple's iphone platform uses objective-c as the development of native language, Objective-c's kernel is C language and implements some features of OOP based on C language.

Objective-c is an extension of the C language, which is similar to the predecessor Better-c of C + +, which implements a garbage collection mechanism similar to Java in the new version of Objective-c, but the resource limits based on the iphone platform

System, the iphone platform does not support garbage collection mechanisms.

(ii), primary knowledge objective-c

1, the composition of cocoa

Apple offers cocoa components such as cocoa, Carbon, QuickTime, and OpenGL as a framework set:

(1), foundation Framework (there are many useful, data-oriented low-level classes and structures);

(2), application Kit (also known as AppKit) framework (contains all user interface objects and advanced classes, such as NS ...), and a support framework kit, including core animation and core Image.

2,NSLog equivalent to the C language of printf ()

NSLog (@ "Hello objective-c"); //  NSLog (@ "is%d and%d different?%@",5,5   // Note%@: This format is used when outputting any object value using NSLog

< Span style= "padding:0px; margin:0px; line-height:25px; " > 3, BOOL uses 8-bit storage, yes is defined as 1,no defined as 0, and greater than 1 is not yes, unlike standard C.

If you accidentally assign an integer value longer than 1 bytes to bool, only the lower eight bits are intercepted

Second, objective-c Common grammar explanation

(1), header file reference use #import "file name" or #import < file name > form to ensure that each header file is included only once;

(2), class declaration to @interface class name: The beginning of the inheriting class, at the end of the @end, the class implementation with the @implementation class name begins with the @end end;

(3), instance method, which is a member method, add a minus sign (-) before the method name, and a class method that adds a plus sign (+) before the method name;

( 4),

The result is that the object invokes the corresponding instance method defined in the class.

(5) Keyword: keywords are basically beginning with @, for example: @public, @private, @end, etc.

(6), String starts with @

@" Hello "    // This one is the OC string " Hello "       // This is a C-language string


(7) Other syntax:

A, basic data type: char, int, float, double, BOOL (yes\no)

B, nil: equivalent to the C language of NULL, which is 0

C, basic statement: Looping statements (do while, while, for), conditional statements (if, if-else, switch)

D, Notes://And/*.......*/

Third, OC development process (when running on the terminal)

1, write the OC source file:. m,. C

2. Compiling: Cc-c xxx.m xxx.c

3, Link: cc XXX.O xxx.o-framework Foundation (only use the foundation framework to add-framework Foundation)

4, run:./a.out

The overall process is:. m (source file) ———— compile ————. O (target file)--Link ——— "a.out (executable file)"

Iv. role of #import in OC and main header file

OC, #import的作用跟C语言中的 # include, is the content of the copy file, the difference is:#import can automatically prevent the contents of the file is copied repeatedly.

The main header file is the main header file, the name is usually the same as the framework name, including all other header files in the framework, such as: The main header file name of the foundation framework is FOUNDATION.H,OC only need to include

The foundation framework Master header file allows you to use all of the member variables, methods, and methods implemented in the framework of the Foundation .

V. Design of class in OC

1. Structure of Class: Class name, property, behavior

Class is an overview of the same properties for all objects.

(1) Specification of class name naming:

1th: The first letter of a class name must be uppercase

2nd: cannot have the underline

3rd: When there are more than one English word, use the Hump logo (that is, the first letter of each word capitalized)

(2) Complete write a class that must contain the declaration of the class and the implementation of the class

Declaration of class: @interface class Name

@end

Implementation of class: @implemention class Name

@end

(3) A declaration is used to: Declare an object's properties, behavior

1>, OC, the declaration of a member variable must be written within {}, and the Declaration of the method (behavior) must be written outside {}.

2>, when declaring a member variable, you cannot assign a value to a member variable, only declare it.

3>, when declaring methods (behavior), be aware that, as long as the method of the OC object must start with a minus sign-and any data type in the OC method must be enclosed in parentheses ().

(4) Attention points for the use of classes

1> in OC, to perform some behavior, write a bracket [behavior performer's behavior name]

2> using classes to create objects

3>, class Invocation: [Method of Object object created]

The preliminary experience of the Dark Horse programmer--objective-c's--oc

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.