1. OC Introduction
Objectuve-C is short for OC, where objective refers to object-oriented, that is, object-oriented C.
- Based on the C language, a minimum object-oriented method is added (the essence of object-oriented syntax is retained)
- 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;
}
- 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.
- You can use OC to develop Mac OS X and iOS applications.
2. OC syntax Overview
(1) OC keywords
- Basically, all keywords 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
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
- The OC program is fully compatible with the C language.
- Basically, all keywords start with @, and a few special keywords do not start.
- The OC string starts.
- 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 ).
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
- Compile CC-C main. m
- Link CC main. o
- Run./A. Out
This section focuses on
- OC program entry: Main Function
- 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
- CC-C main. m
- CC main. O framework Foundation
(The-framework foundation must be added only when the foundation framework is used)
- Run./A. Out
3. Differences between nslog and printf
- To use nslog, # import <Foundation/Foundation. h>
- To use printf, # include <stdio. h>
- 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
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, 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>
- Development of essential frameworks for OC, IOS, and Mac programs
- This framework contains many common APIs (application interfaces)
- The Framework contains many header files. If you want to use the entire framework, you can include its main header file.
- 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
- 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)
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
- C language uses printf function to output content; OC language uses nslog function to output content, and nslog output content will automatically wrap.
- There must be no space between @ and "" In the nslog output statement.
- -Framework foundation must be added only when the foundation framework is used.
- # Import can automatically prevent file content from being copied multiple times and will be used later # import.
- 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