OC Language Overview

Source: Internet
Author: User

1.OC Language Overview

1>foundation.h we call it the main header file, and the header file of all the tools in the toolbox is copied, we only need to import the main header file to use all the tools in the toolbox, to avoid importing a corresponding header file for each use.

Toolbox Address:/applications/xcode.app/contents/developer/platforms/iphoneos.platform/developer/sdks/iphoneos.sdk/ System/library/frameworks

Rule: All primary header files have the same name as the Toolbox

All the main header files are header files that import all the tools in the Toolbox

The difference between 2>import and include:

The import function, like the include, is to copy the right file to the current import location

In order to reduce the burden of the programmer, prevent duplicate import, avoid the programmer to write the header file, then OC gives a new preprocessing command import

Import Advantage: Duplicate copies are automatically prevented

Because OC is fully compatible with C, you can write C code in the OC program

And you can combine the C language source file with the OC source file to generate the executable file

The difference between 3>printf and NSLog:

NSLog will wrap automatically.

NSLog will append some system information when the content is output

NSLog and printf receive different parameters

2. First OC class

In OC to define a class also sub-declaration and implementation, that is, we define the class in OC, that is, the Declaration and implementation of the Write class

1> How to write a class declaration

Start with @interface, @end end, and then write the name of the object in the same place as the class name, which can be

Note: The first letter of the class name must be capitalized

The purpose of declaring a class is to tell the system what properties and behaviors we have in this class

Thing name: IPhone

Properties: Color, model, cpu, size = = member variable

Behavior: Call, send text messages, surf the internet, get native Information = = Member method

The properties in the OC class declaration can only be written in {} between @interface and @end

For example:

@interface Iphone:nsobject

{

@public

float _model; Model 0

int _cpu; Cup 0

Double _size; Size 0

int _color; Color 0

}

Note: When writing the properties of the 0C class, it is recommended to precede all the names of the properties with _ (the reason is again in the back, that is, the getter and Seter name problem).

Behind the class name: NSObject is for our class to have the ability to create objects, and also a parent class of classes.

Note: The properties in the OC class are not directly accessible by default, which is the protected modifier

As long as the properties in the class are exposed, you can then manipulate the properties in the object directly through a pointer to the struct body

n (int argc, const char * argv[]) {

2> How to create an object from a class

In OC you want to create an object from a class, you must send a message to the class (like calling a function in C)

How do I send a message?

In OC, if you want to send a message, write the [class name/object name Method name] first.

What message is sent (what method is called) to create an object?

New

Whenever a new method of a class is called through a class, that is, when a message called New is sent to the class

3 things will be done inside the system.

1. Create an object for iphone class to allocate storage space

2. Initialize the properties in the object created by the iphone class

3. Returns the address of the object created by the iphone class

Received the address of the iphone object via an iphone type pointer

If you use a pointer to save an object's address, then we call this pointer a type of object

Using the iphone type pointer to save the address of the iphone object, we'll call the iphone type pointer p as the iphone object

The class in OC is essentially a struct, so p is actually pointing to a struct

3> the difference between a function and the behavior in a class

The behavior in OC, like a function in C, is the code used to hold a specific function

The C language defines a function, which is divided into declarations and implementations, and declarations are written in. h, and implementations are written in. c

OC defines a method, also divided into declarations and implementations, declarations written in @interface, implementation written in @implementation

The functions of C are divided into two types: intrinsic and external functions

The methods in OC are also divided into two kinds; Class methods and Object methods

Class methods can only be called with the class name, and object methods can only be called with objects

The class method in OC is represented by +, and the object method in OC is represented by

The law of writing C language functions: 1. Determine the function name 2. Determine the parameter 3. Determine the return value 4. Determine the return value type

There are rules for writing OC methods, which are exactly the same as the C language.

() The role of:

Note: The method in OC, if no formal parameter does not need to write (), but write directly one;

Why is there no formal parameter in OC that does not need to be written ()? Because the OC method () has a special purpose, the OC Method () is used to extend the data type

Functions of the 4>c language:

No return value with no parameters

There are no parameters for the return value.

There are parameters for the return value.

There are no parameters for the return value

There is no parameter to the return value, read the text message

-(char *) loadmessage;

There are parameters of the return value, call

int signal (int number);

Note: If the method in OC has parameters, then the data type of each parameter must be preceded by one:

Note: The method name of the current parameter is signal:

A colon is also part of the method name

-(int) signal: (int) number;

With a return value and with multiple parameters, send a text message

int sendMessage (int number, char *content);

The method name of the following method is SendMessage::

-(int) SendMessage: (int) Number:(char *) content;

To improve our reading, the OC method allows us to add a label to each parameter to illustrate the meaning of the current parameter

Note: Tags are also part of the method name

[Object Message Name (method name)]

Note: NSLog in OC is not very good for C language string support, if the return is Chinese C language string may output is garbled, also may have nothing to output

If you define a class method, the method of the class is exactly the same as the object method, except for the preceding-number difference, you only need to change the number of the object method to +, then define a class method

The difference between a 5> class method and an object method

0. Object methods begin with-

class methods begin with +

1. Object methods must be called with an object

Class methods must be called with a class

2. Properties (member variables) can be accessed directly in the object method

No direct access to properties (member variables) in class methods

3. Class methods and object methods can be called to each other

The class method can be called directly in the 4.1 object method

Indirect invocation of object methods in Class 4.2 methods (Note: This is not recommended)

Other class methods can be called directly in Class 4.3 methods

Object methods can be called directly in 4.4 object methods

Application Scenarios for class methods

If a property (member variable) is not used in a method, the class method can be used to

Class methods perform more efficiently than object methods

Class methods are typically used to define tool methods

String Lookup

File operations

Database operations

6>isa

The address that is returned when the object is created is actually the address of the No. 0 property of the class.

However, it is important to note that the No. 0 property of the class is not the _age that we write, but a property called Isa

ISA is a pointer, accounting for 8 bytes

In fact, the class is also an object, which means that person is also an object

Usually what we call creating objects is actually creating a new object from a Class object.

The class object is created automatically by the system, which holds all the methods of the current object.

The instance object is created by the program itself manually through new, and an Isa pointer in the instance object points to the class object that created it.

OC Language Overview

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.