First, learn about OC before
Introduction to OC Syntax
- Keywords begin with @;
- The string starts with @, such as NSLog (@ "I love iOS.");
- Import can automatically prevent the contents of the file from being copied repeatedly;
If you are editing the OC program at the terminal, you must note the following compiled link directive:
- Compiling cc–c main.m
- Link cc main.o–framework Foundation
- Run./a.out
The role of the Foundation framework:
- Develop the necessary framework for OC, IOS, MAC programs
- This framework contains a number of commonly used APIs (application programming interfaces)
- The framework contains a lot of header files, if you want to use the contents of the entire framework, including its main header file can be
Two, a simple example of creating objects based on the OC class
1 #import<Foundation/Foundation.h>2 //definition of car class3 @interfaceCar:nsobject4 {5 @public6 intSpeed ;7 intWheels;8 9 }Ten- (void) run; One @end A //The realization of car class - @implementationCar - the- (void) run{ -NSLog (@"A car with%d wheels is runing"); - } - @end + - intMain () + { ACar *p =[carNew];//the P pointer points to the new object created atP->speed = -; -P->wheels =4; -[P run];//sends a run message message mechanism to the car object that was created -}
Note The above procedures:
- Declare the function in the Declaration, in the implementation of the implementation of writing methods;
- Methods and functions are not a thing;
- The property value is initialized to 0 by default;
- Add "NSObject" after the class you created to allow car to have the ability to create objects;
- car* p =[car New] is a p pointer pointing to the in-memory Car object;
- [P run] sends the run message, the message mechanism;
The benefits of object-oriented encapsulation:
- Closer to the way people think;
- You just need to focus on the object and don't need to follow the steps.
First knowledge of Objective-c