Dark Horse Programmer--from C language to OC

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

iOS development (including apps on iphone and ipad) uses the language objective-c, the OC language, which was born in 1986 and is an older language. OC is fully compatible with the C language, which means that all of the syntax features in C are available in OC, so many libraries and functions developed in C can be used directly in the OC language (which is why most of the keywords in the OC language begin with @). The main purpose is to differentiate the C and OC codes in the same file. But OC's basic grammatical staff has a different part of the C language. This article mainly introduces some of the confused parts of C language development to OC Language Development transformation Process:

1. What is the. m file extension?

when Objective-c was first born, the extension. m stands for message, referring to A major feature of the objective-c. Because OC is an object-oriented language different from C (Process oriented), the methods of a class or an object are passed in the form of a message, rather than simply a function, so the code that passes the message everywhere in the Objective-c language (which is actually called the class method), so the OC source file has an. m extension.

2. The bool type is added to the base data type

The bool type is a data type with only true and false values, which is very common in other languages, and objective-c to compensate for the lack of bool types in C, a new type of bool is defined using the Typedefine keyword in C language:

[OBJC]View Plaincopy
    1. typedef SIGNED Char BOOL;

and two kinds of values are defined

[OBJC]View Plaincopy
    1. #define YES ((BOOL) 1)
    2. #define NO ((BOOL) 0)


This allows you to use the bool type when it comes to judging the true and false, without having to use 0 and 1 without the actual meaning.

3.nil type

The nil type in OBJECTIVE-C is equivalent to the null type in the C language, and the command key in Xcode enters the. h header file to see a macro definition for the nil type

[OBJC]View Plaincopy
    1. # define NIL __darwin_null

[OBJC]View Plaincopy
    1. #define __DARWIN_NULL ((void *) 0)


As defined by the above two paragraphs, the nil in OC is basically the same as the Null in C language.

But since OC is an object-oriented language, nil can not only represent the meaning of 0 in the basic data type and pointer type, but can also represent a class type that has no value, so the nil usage in OC is richer, so it is recommended to use nil in the OC program instead of using NULL in the C language.

4. #import预处理命令

function is the same as # include, used to copy the contents of a file, but why not use the # include and the new #import.

Because the #import command in OC has a richer function

#import是GCC编译器提供的Xcode在编译Objective-C, C, and C + + programs use it #import可保证头文件只包含一次, regardless of how many times the command actually appears in that file.

#import可以自动防止文件内容被拷贝多次, you don't have to add the following tedious preprocessing instructions to your header file as you would when writing a C program.

[OBJC]View Plaincopy
    1. #ifndef _stdio_h_
    2. #define _stdio_h_
    3. #endif

Therefore, you can safely discard the # include directive when you write the OC program later.

Keywords and string types in 5.OC

Most of the keywords in the OC language begin with @, and the most common keywords are the following:

[OBJC]View Plaincopy
  1. @interface,@implementation,@end
  2. @public,@protected,@private,@selector
  3. @try,@catch,@throw,@finally
  4. @protocol,@optional,@required,@class
  5. @property,@synthesize,@dynamic
  6. Self,super,ID, _cmd, __block, __strong, __weak,


The string in the OC language differs from the string in C, which is not a simple character array, but a wrapper type (class type) that begins with @

For example @ "Hello" is a string in OC, and "Hello" is a string in C language

6. Basic Output Function NSLog

The main differences between the OBJECTIVE-C and the more advanced output functions in the C language are as follows NSLog

Ønslog receives the OC string as a parameter, printf receives the C language string as a parameter

Ønslog after output, does not wrap automatically after printf output

Ø use of NSLog requires #import <Foundation/Foundation.h>

Ø using printf requires #include<stdio.h>

7.Foudation Frame

In the OC language, the classes, structs, and functions that are used are defined in the. h file (and implemented in the. m file), and some companies or individuals develop frameworks with specific functions, with many. h and. m files, and use these frameworks or frameworks for certain functions, A class or struct must use the #import keyword to introduce a header file that contains them, and a primary header file named after the frame name contains declarations for all functions, classes, and structs in the framework (typically adding a primary header file when you do not know which header file to add specifically), such as

#import <Foundation/Foundation.h>

means to add a Foundation.h file under the foundation framework where the frame name and header file are separated by "/".

Almost all OC programs are added to the foundation framework because

Øfoundation developing the necessary framework for OC, IOS, MAC programs

Ø This framework contains a number of commonly used APIs (application programming interfaces)

8. How to compile and link OC files

As in the previous C language programming, want to let OC program run up, also need three steps, namely: Compile, link, execute. But it's a little bit different from the C language.

is to add a frame (indicating the –framework parameter) used in the OC program during the link phase, for example:

Ø Compile: cc–cmain.m test.m

Ø Link: cc main.otest.o–framework Foundation

Ø run:./a.out

9. A complete OC program

Summarize the previous description, you can no longer be unfamiliar with the OC program, below is a complete OC version of Hello World

[OBJC]View Plaincopy
    1. #import <Foundation/Foundation.h>
    2. int main (int argc, const Char char* argv[])
    3. {
    4. NSLog (@ "Hello, world!");
    5. return 0;
    6. }


Open the terminal CD to the directory where the file is located, and then

Compile:

[Plain]View Plaincopy
    1. Cc-c HELLO.M


Link:

[Plain]View Plaincopy
    1. CC Hello.o-framework Foundation


Perform:

[Plain]View Plaincopy
    1. ./a.out


Display on the terminal screen:

Hello, world!.

Dark Horse Programmer--from C language to OC

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.