Overview of the basic syntax for Blackhorse programmers 00-oc

Source: Internet
Author: User

1. OC Introduction

Objectuve-C is short for OC, where objective refers to object-oriented, that is, object-oriented C.

 

  1. Based on the C language, a minimum object-oriented method is added (the essence of object-oriented syntax is retained)
  2. Fully compatible with C language

 

The following code is written in the OC source file (. M file), indicating that the OC program is fully compatible with the C language.

 

# Include <stdio. h>

// OC program entry: Main Function

// The OC program is fully compatible with the C Language

Int main (intargc, const char * argv [])

{

Printf ("1st OC Programs \ n ");

Return 0;

}

 

  1. The C language code or even c ++ code can be incorporated into the OC code.

 

For more information, see

In the. M file, both OC code and C code can be written;

The. MM file can write c ++ code.

 

  1. You can use OC to develop Mac OS X and iOS applications.

 

2. OC syntax Overview

 

(1) OC keywords

  1. Basically, all keywords start @.
  1. The following lists some common keywords. You can just take a look at them. You don't have to go over the information to find their meanings.

@ Interface, @ implementation, @ end

 

@ Public, @ protected, @ private, @ Selector

 

@ Try, @ catch, @ throw, @ finally

 

@ Protocol, @ optional, @ required, @ Class

 

@ Property, @ synthesize, @ dynamic

 

A few special keywords do not start:

 

Self, super, ID, _ cmd, _ block, _ strong, _ weak,

 

 

 

(2) The string starts @

For example, @ "hello" is a string in OC, while "hello" is a string in C.

 

(3) Other syntaxes

 

 

 

3. OC Program Development Process

Similar to C:

 

 

Purpose of Compilation: Check the syntax. If the syntax is correct, the compilation is successful and the. O target file is generated;

Function of link: combines all the associated. O target files in the project with the C language function library to generate an executable file (A. out ).

 

 

This section focuses on

  1. The OC program is fully compatible with the C language.
  1. Basically, all keywords start with @, and a few special keywords do not start.
  2. The OC string starts.
  3. Purpose of Compilation: Check the syntax. If the syntax is correct, the compilation is successful and the. O target file is generated.
  4. Function of link: combines all the associated. O target files in the project with the C language function library to generate an executable file (A. out ).

 

2. 1st OC programs

 

1. Code Writing

Like the C language, the entry of the OC program is the main function, but it is written in the source file (. M file) of the OC.

For example, in the first OC main. M file (the file name can be Chinese ):

 

# Include <stdio. h>

// OC program entry: Main Function

// The OC program is fully compatible with the C Language

Int main (intargc, const char * argv [])

{

Printf ("1st OC Programs \ n ");

Return 0;

}

 

2. Terminal commands

  1. Compile CC-C main. m
  2. Link CC main. o
  3. Run./A. Out

 

This section focuses on

  1. OC program entry: Main Function
  2. Terminal commands
    • Compile CC-C main. m
    • Link CC main. o
    • Run./A. Out

 

 

3. 2nd OC programs

 

C language uses printf function to output content; OC language uses nslog function to output content.

 

1. Code Writing

The second OC program is as follows:

# Import <Foundation/Foundation. h>

 

Int main ()

{

// Nslog output will automatically wrap

// No space is allowed between @ and ""

Nslog (@ "2nd OC programs !!! ");

Return 0;

}

 

2. Terminal commands

  1. CC-C main. m
  2. CC main. O framework Foundation

(The-framework foundation must be added only when the foundation framework is used)

  1. Run./A. Out

 

3. Differences between nslog and printf

 

  1. To use nslog, # import <Foundation/Foundation. h>
  2. To use printf, # include <stdio. h>
  1. Nslog receives OC strings as parameters, and printf receives C language strings as parameters.
  2. Nslog will automatically wrap after output, but not after printf output

 

4. # role of Import

 

  1. Like # include, it is used to copy the content of a file.
  1. It can automatically prevent the file content from being copied multiple times, which means that the following preprocessing commands are not added to the header file.

# Ifndef _ stdio_h _

# DEFINE _ stdio_h _

# Endif

 

// Nsobjcruntime. h contains the nslog function declaration

# Import <Foundation/nsobjcruntime. h>

 

<> In/, the previous part indicates the framework, and the later part indicates the. h header file in the framework.

 

5. Role of the foundation framework

 

# Import <Foundation/Foundation. h>

 

  1. Development of essential frameworks for OC, IOS, and Mac programs
  2. This framework contains many common APIs (application interfaces)
  3. The Framework contains many header files. If you want to use the entire framework, you can include its main header file.
  4. Storage path of the foundationl framework:
    • Right-click xcode. app --> display Package content
    • /Applications/xcode. APP/contents/developer/platforms/iphoneos. Platform/developer/sdks/iphoneos. SDK/system/library/frameworks/Foundation. Framework/Headers
  1. Master header file
    • Main header file: The main header file, which is generally the same as the framework name and contains all other header files in the framework.
    • The header file name of the foundation framework is foundation. h.
    • You only need to include the main header file of the foundation framework to use the entire framework.

Note: Do not add or delete anything.

6. Use of the bool type (no bool type in C language)

  • Bool type nature

The essence of the bool type is actually char.

Typedef signed Char bool

  • The bool type can be set to yes or no.

# Define Yes (bool) 1

# Define no (bool) 0

  • Bool output (as an integer)
  • NSLog(@"%d %d", YES,NO);

Write the following code:

1 # import <Foundation/Foundation. h> 2 3 // Return Value Type of bool, return No 4 bool test (boolmybool) 5 {6 return no; 7} 8 9 int main (intargc, const char * argv []) 10 11 {12 bool B = yes; 13 bool b2 = no; 14 bool B3 = 1; // yes15 bool B4 = 0; // no16 17 nslog (@ "B = % d", B); // The result is B = 118 19 nslog (@ "b2 = % d", B2 ); // The result is b2 = 020 21 nslog (@ "B3 = % d", B3); // The result is B3 = 122 23 nslog (@ "B4 = % d ", b4); // The result is B4 = 024 25 nslog (@ "% d", test (yes); // The result is 026 27 return 0; 28 29}

 

  

This section focuses on

  1. C language uses printf function to output content; OC language uses nslog function to output content, and nslog output content will automatically wrap.
  2. There must be no space between @ and "" In the nslog output statement.
  1. -Framework foundation must be added only when the foundation framework is used.
  2. # Import can automatically prevent file content from being copied multiple times and will be used later # import.
  3. You only need to include the main header file of the foundation framework to use the entire framework.

 

Overview of the basic syntax for Blackhorse programmers 00-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.