Part 1 Introduction to the 1.0 OC language, 1.0oc

Source: Internet
Author: User

Part 1 Introduction to the 1.0 OC language, 1.0oc

1. OC Introduction

Ii. OC syntax Preview
1. Keywords

@ Interface, @ implementation, @ end

@ Public, @ protected, @ private, @ selector

@ Try, @ catch, @ throw, @ finally

@ Protocol, @ optional, @ required, @ class

@ Property, @ synthesize, @ dynamic

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.

Other syntaxes

4. 1st OC programs
1. Code Writing
Like the C language, the entry to the OC program is still the main function, but it is written to a. m file. For example, it is written to a main. m file (the file name can be Chinese)

# Include <stdio. h> int main () {printf ("1st OC Programs \ n"); return 0 ;}

2. Terminal commands
Compile cc-c main. m
Link cc main. o
Run./a. out

5. 2nd OC programs
1. Code Writing
Different from the C language, use the NSLog function to output content.

# Import <Foundation/Foundation. h> int main () {NSLog (@ "2nd OC programs"); return 0 ;}

2. Terminal commands
Compile cc-c main. m
Link to cc main. o-framework Foundation
Run./a. out

3. Differences between NSLog and printf
NSLog receives OC strings as parameters, and printf receives C language strings as parameters.
NSLog will automatically wrap after output, but not after printf output
To use NSLog, # import <Foundation/Foundation. h>
To use printf, # include <stdio. h>

4. # role of import
Like # include, it is used to copy the content of a file.
It can automatically prevent the file content from being copied multiple times, so that the following preprocessing commands are not required in the header file.
# Ifndef _ STDIO_H _
# Define _ STDIO_H _
# Endif

5. Role of the Foundation framework
Development of essential frameworks for OC, iOS, and Mac programs
This framework contains many common APIs (application programming interfaces)
The Framework contains many header files. If you want to use the content of the entire framework, you can include the main header file.
# Import <Foundation/Foundation. h>

6. Use of BOOL
BOOL type nature
Typedef signed char BOOL;
The BOOL type has two values: YES and NO.
# Define YES (BOOL) 1
# Define NO (BOOL) 0
BOOL output (used as an integer)
NSLog (@ "% d", YES, NO );

6. 3rd OC programs
1. Development of multiple. m Files
It is the same as developing multiple. c files in c.
1. 0. Write 3 files
Main. m

#import "one.h"int main(){    test();    return 0;}

One. h

void test();

One. m

# Import <Foundation/Foundation. h> void test () {NSLog (@ "test function called ");}

1. Terminal commands
Compile: cc-c main. m test. m
Link: cc main. o test. o-framework Foundation
Run:./a. out

2. Mixed Development of. m Files And. c files
2.0 compile 3 files
Main. m
# Import "one. h"
Int main ()
{
Test ();
Return 0;
}
One. h
Void test ();
One. c
# Include <stdio. h>
Void test ()
{
Printf ("the test function \ n" is called ");
}

2.1 terminal commands
Compile: cc-c main. m test. m
Link: cc main. o test. o
Run:./a. out
(If the Foundation framework is not used, you do not need-framework Foundation)

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.