objective-c-Basic Knowledge

Source: Internet
Author: User

Pre-preparation for OC language

I. INTRODUCTION of OC

OC language on the basis of the C language, the addition of a layer of the smallest object-oriented syntax, fully compatible with the C language, in the OC code, can be mixed with C, or even C + + code.

You can use OC to develop applications for Mac OSX platforms and iOS platforms.

Extended Name: C language-.c OC language.-M compatible c++.-mm

Note: In fact, C language and OC or even any language is just for us to achieve some functions, to achieve some effect and the use of tools, aside from the grammatical differences, I think the most important thing is to solve the problem when the angle and method of thinking is different, but this also constitutes the importance of learning a language.

Second, Syntax preview

(i) Keywords

Basically all of the keywords are at the beginning of the (in order to distinguish with the C keyword), such as @interface @implementation @public, and so on, a small part does not start with @, such as id,_cmd, etc.

(b) The string begins with @

C-language string: "Hello"

OC language string: @ "Hello"

(iii) Other grammatical

Basic type: 5 types, added Boolean type

Nil equals null, which is 0.

Screen output: NSLog (@ "Hello");//Wrap Line

NSLog (@ "Age is%d", 2);

Third, OC program development process

#import预处理指令有两个作用: (1) As with # include, the Copy file contents (2) can automatically prevent the contents of the file from being copied repeatedly

The program compiles the connection process:

source file (. m)---(compile)----> destination file (. 0)-----(link)----> executable (. Out)

Foundation Framework. What should I do if I want to use all the header files in the frame? Contains the primary header file for the framework. The primary header file is the most important header file in a framework, with the main header filename and frame name consistent for each frame.

such as #import<foundation/foundation.h>

Run the following procedure:

(1) Write the OC source file. M. C

(2) Compiling files cc-c xx.m xxx.c

(3) Link cc xx.o xxx.o-framework Foundation

(4) run./a.out

Iv. Types of Supplements

Int Main ()

{

BOOL B=yes;

BOOL B1=no;

BOOL b2=1;//YES

BOOL b3=2;//NO

NSLog (@ "%i", b);

}

The bool type is consistent with other types of usage, and the nature of the bool type is char type, defined as follows:

Typedef signed Char BOOL

Macro definition:

#define YES (BOOL) 1

#define NO (BOOL) 0

The output of a Boolean type is generally used as an integer.

V. Object-oriented

OC language is object-oriented, the C language is process-oriented, object-oriented and process-oriented are just two ways to solve the problem, the process is concerned with the steps involved in solving the problem, object-oriented is to design a class that can achieve the functions needed to solve the problem.

Terminology: Oo Object-oriented, OOP OO programming

Vi.. Class

(i) About class

Class is designed to focus on only three things: class names, properties, and methods

Note: General nouns are classes, objects with the same properties and behavior can be abstracted as a class, the class name is one of the identifiers, need to conform to the specification, usually the first letter of the class name is capitalized, and cannot have an underscore, if there are more than one word use hump identification. In the classification of methods, the general practice is who is most familiar with this method and then divides this method to WHO. In OC, the invocation of an object to a method is called a message mechanism, that is, what message is sent to the given object.

(b) Simple memory analysis

class to create objects, each object occupies a certain amount of storage space in memory, each object has a separate member variable belonging to its own, all of the object common class member methods, methods in the whole memory only one copy, the class itself in memory occupies a portion of storage space, the method of the class is stored here.

Within each object, there is a default ISA pointer to the class used by this object.

[P eat]; indicates that a eat message is sent to the object that P points to, calling the Eat method of the object, where the object will follow the internal ISA pointer to find the method stored in the class.

ISA is a hidden pointer in an object that points to the class that created the object.

(iii) Declaration and implementation of the class

1) Declaration of class:

This declares a person class that has a @public decorated property (member variable) and an object method put.

2) class implementation: The declaration of a class can be understood as the implementation of a method in a class

3) class invocation: The main function first creates an object of type person (call Alloc to allocate storage space, then initializes the Init method to 0), and defines a person type pointer to the object created, and then initializes the value of the object member variable _age to 20. The object's Put method is then called, and the printout is printed.

4) Practice, create a person class.

Declaration of the class:

Class is implemented as follows:

Class is called as follows:

(d) Common mistakes

(1) @interface @end and @implementation @end cannot be nested with

(2) Only the declaration of the class has no implementation of the class

(3) Missing write @end

(4) Declaration nesting of two classes (can be scrambled in order)

(5) member variable not written in {}

(6) The declaration of the method is written in {}

(7) Initialize member variables of a class at declaration time, and note that member variables cannot exist independently from the object

(8) method cannot be called like a function

(9) member variables and methods can not be modified with static and other keywords, do not confuse with C language

(10) The implementation of the class can be written behind the Mian function, as long as there is a declaration before use can

Vii. objects and functions of OC

OC objects are fundamentally different from functions:

(1) The implementation of the method can only be written in @implementation @end, the declaration of an object method can only be written in @interface @end Middle

(2) The object method begins with the-number, and the class method starts with the + sign

(3) An object method can only be called by an object, and a class method can only be called by a class and cannot be called as a function

(4) The function belongs to the entire file and can be written anywhere in the file, including @implementation @end, but written in @interface @end is not recognized, the declaration of a function can be inside the main function or outside of the main function.

(5) Object method collation \ Object All

(6) The function call does not depend on the object

(7) The member variable of an object cannot be accessed directly from the member variable name within the function

Viii. design of classes and methods

Tool class: Basically there is no member variable, the method is basically the class method.

Note: You can call a class method in an object method.

Requirements: Design a tool class: A calculator class that requires (1) to return π, (2) calculates the sum of two integers, and (3) calculates the square of an integer.

Declaration part of a class

Implementation part of the class

Test procedure:

objective-c-Basic Knowledge

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.