OC (objective-C for short)
Based on the C language, an object-oriented syntax with the smallest scope is added (the most essential part of object-oriented is retained, and the content of OC is not much java, java does not have much content than C ++, and C ++ is the most complicated)
OC is fully compatible with C language, and C and OC can be mixed. The C language code can be mixed into the OC code (provided that the OC source file extension is. m), even c ++ Code (not all source files can contain c ++ Code, only the source file extension is. MM source file can contain c ++ code ). You can use OC to develop Mac OS X and iOS applications.
Keywords
Basically, all keywords start with @, and some common 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
The string starts @.
For example, @ "hello" is a string in OC, while "hello" is a string in C.
The @ and string must be close to each other.
Other syntaxes
OC Program Development Process
Edit, compile, and link (merge all the associated. O files and add the function library) to run the program. Similar to C, compile the. M file first, then compile, link, and run the compiler.
Fully compatible with C language
Like the C language, the entry of the objective-C program is still the main function, but it is written to a. m file (the file name can be Chinese ).
1 # include <stdio. h> 2 3 // The entry function of the OC program is also the main function 4 5 Int main (void) 6 7 {8 9 printf ("the first OC program! The OC source file is fully compatible with the C language! "); 10 11 return 0; 12 13}
Compile CC-C main. M, link CC main. O, and run./A. Out.
Output content using nslog Function
1 # import <Foundation/nsobjcruntime. h> 2 // not # include, but # import3 int main (void) 4 {5 // nslog output automatically wrap 6 nslog (@ "second OC program !! "); 7 return 0; 8}
Compile CC-C main. m
Link to CC main. O-framework Foundation
Note: the link to the OC program only automatically links the C library functions. When the OC library functions need to be linked, write-framework Foundation as long as the foundation framework is used, you have to write.
Run./A. Out
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
# Import <Foundation/Foundation. h> is required to use nslog, which contains the nslog function declaration. You do not need to remember it. If you cannot remember it, compile it. If an error occurs, you will be prompted about what is missing.
Only the main header file of the framework can be included. The effect is equivalent to that of all header files in the framework. The header file is in xcode, and the path is very deep.
To use printf, # include <stdio. h>
# Role of import (preprocessing)
1. Like # include, it is used to copy the content of a file.
2. The file content can be automatically prevented from being copied multiple times, that is, the header file does not need to be the same as C. Add the following preprocessing commands.
# Ifndef xxx
# Define xxx
# Endif
Role of the foundation framework
An essential framework for developing 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>
Use of bool (can be used as an integer)
Nature of the bool type: typedef signed Char bool;
There are only two bool-type variables: yes and no.
# Define Yes (bool) 1
# Define no (bool) 0
Bool output (used as an integer); nslog (@ "% d", yes, no); // 1 0
C does not have a boolean type, C ++ does, and OC does. However, there is a difference between the Boolean value of OC and C ++. In C ++, all non-0 values are true, and false values are 0 values. However, in object-C, the value 1 must be true, and the macro is defined as yes, and 0 is false, and the macro is defined as No. Therefore, the following code cannot obtain the desired running sequence.
1 #import <Foundation/Foundation.h> 2 BOOL isBool(int, int); 3 4 int main(void) 5 { 6 if (isBool(5, 1) == YES) { 7 NSLog(@"ok"); 8 } 9 return 0;10 }11 BOOL isBool(int x, int y)12 {13 return x - y;14 }
4 is not 1, and will never be equal to Yes (Yes is 1) in OC. Note: bool capital, Yes No Is capital, yes is 1, no is 0
Development of multiple. m Files
Similar to the development of multiple files in C language, the definition of constants or function declaration of C multi-file development is written in XXX. h file, function definition written in XXX. c file, main file. c write the main program and function call, and # include XXX. h file, compile the main file main. C and XXX. c file.
Write 3 OC 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 ");}
Compile: CC-C main. m test. m (the two files must be compiled together)
Link: CC main. O test. O-framework Foundation
Run:./A. Out
Mixed Development of. m Files And. c 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 ");}
Compile: CC-C main. m one. c
Link: CC main. O test. O (if the foundation framework is not used, no-framework foundation is required)
Run:./A. Out
Objective-c Study Notes (1)