1. OC Introduction
- Based on the C language, a minimum object-oriented syntax is added.
- Fully compatible with C language
- The C language code or even c ++ code can be incorporated into the OC code.
- You can use OC to develop Mac OS X and iOS applications.
Ii. OC syntax preview 1. Keywords
- Basically all the keywords are @Start
- 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
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
Iii. OC Program Development Process
Similar to C:
Iv. 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)
1 # include <stdio. h> 2 3 int main () 4 5 {6 7 printf ("1st OC Programs \ n"); 8 9 return 0; 10 11}
2. Terminal commands
- Compile CC-C main. m
- Link CC main. o
- Run./A. Out
V. 2nd OC programs 1. Code Writing
Different from the C language, use the nslog function to output content.
1 # import <Foundation/Foundation. h> 2 int main () 3 {4 nslog (@ "2nd OC programs"); 5 return 0; 6}
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
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 %d", YES, NO);
Vi. 3rd OC programs 1. Development of multiple. m Files
It is the same as developing multiple. c files in C.
1) write 3 files
1 #import "one.h"2 int main()3 {4 test();5 return 0;6 }
void test();
1 # import <Foundation/Foundation. h> 2 void test () 3 {4 nslog (@ "test function called"); 5}
2) 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
1) write 3 files
1 #import "one.h"2 int main()3 {4 test();5 return 0;6 }
void test();
# Include <stdio. h> void test () {printf ("the test function \ n" is called ");}
2) terminal commands
- Compile: CC-C main. m one. c
- Link: CC main. o one. o
- Run:./A. Out
(If the foundation framework is not used, you do not need-framework Foundation)
VII. Summary
1. # purpose of import:
1> like # include, copy the file content
2> automatically prevents duplicate copies of File Content
2. # import <Foundation/nsobjcruntime. h>
Nsobjcruntime. h contains the nslog function declaration.
3. Path of the header file of the foundation framework
1> right-click xcode. app --> display Package content
2> xcode. APP/contents/developer/platforms/iphoneos. Platform/developer/sdks/iphoneos6.0.sdk/system/library/frameworks/Foundation. Framework
4. Master header file
1> 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.
2> the header file name of the foundation framework is foundation. h.
3> you only need to include the main header file of the foundation framework to use the entire framework.
5. Running Process
1> write OC source files:. M,. c
2> compile: CC-c xxx. m xxx. c
3> link: cc xxx. o xxx. O-framework Foundation
(The-framework foundation must be added only when the foundation framework is used)
4> Run:./A. Out