I. INTRODUCTION of OC 1. Introduction
It is based on the C language and adds a layer of object-oriented syntax
OC fully compatible with C language
You can mix C code, even C + + code, in OC code
Applications that can use OC to develop Mac OS x platform and iOS platform
2. Keywords
1> basically all the keywords are beginning with @
2> 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
3> string starts with @
For example @ "Hello" is a string in OC, while "Hello" is a string in the C language
4> OC Program Development process
5> is the same as the C language, and the OC's coarse entry is still the main function
The terminal instructions are the same. Compile cc–c main.m; link cc main.o; run./a.out
The difference between 6> NSLog and printf
①nslog receives the OC string as a parameter, printf receives the C language string as a parameter
②nslog after output, does not wrap automatically after printf output
③ to use NSLog #import <Foundation/Foundation.h>
④ using printf requires #include <stdio.h>
7> #import的作用
①, like # include, is used to copy the contents of a file
② can automatically prevent the file contents from being copied multiple times, which means that the following preprocessing instructions are not included in the header file.
#ifndef _stdio_h_
#define _stdio_h_
#endif
The role of the 8> Foundation framework
① developing the necessary framework for OC, IOS, MAC programs
② This framework contains a number of commonly used APIs (application programming interfaces)
③ Framework contains a lot of header files, if you want to use the entire framework of content, including its main header file can be
#import <Foundation/Foundation.h>
Ii. object-oriented and process-oriented thinking
OC is object-oriented and C is process-oriented. Object-oriented and process-oriented are just two different ideas for solving problems
1. Object-oriented and process-oriented differences
1> take a computer to see a movie as an example
A. Process-oriented
① turn on your computer ② play a movie in your computer ③ turn off your computer
B. Object-oriented
① computer (powering on, playing movies, shutting down)
2> differences
①-oriented process is concerned with what steps are required to solve the problem, and object-oriented focus on what objects are needed to solve the problem
② No development experience is difficult to feel their differences, both ideas can achieve the goal of solving problems, but the solution is not the same way
Iii. relationship of Class and object 1. Object-oriented in OC
1> OC classes are equivalent to drawings that describe a class of things. That is, if you want to create an object, first class
2> OC uses classes to create objects, which are the concrete existence of classes
3> Object-oriented problem solving should be to consider which classes need to be designed, and how many objects to create by using classes
2. How to design a class
The design of the class, focusing on only 3 things
1> thing name (class name): Man (person)
2> Properties: Height (height), age
3> behavior (function): Run (run), eat (EAT)
Iv. defining OC Classes and creating OC Objects 1. There are two steps to owning a class: Declaration and implementation of a class
Declaration of the 1> class, declaring the object's properties and behavior
@interface Car:nsobject
{
int speed; Member variable--speed
int wheels; Member variable--Number of wheels
}
-(void) The act of running run;//; a declaration of a method
@end
The implementation of the 2> class,
Implementation of @implementation Car//Class
-(void) Run//method implementation
{
NSLog (@ "The car ran up");
}
@end
2. Create an Object
int main ()
{
Car *c = [car new]; Creates a car object and returns the address of the object, with the object's address stored with the pointer
C->wheels = 3; Accessing member variables with pointers
C->speed = 300; Accessing member variables with pointers
[C run];//Object Invocation method, [Behavior performer behavior name], and also a message mechanism for OC, sending a message to the object pointed to by the pointer
return 0;
}
3. Common errors and points of attention
1> Common Errors
Only the declaration of the class, no implementation of the class
Missed the @end.
@interface and @implementation nesting
Declaration nesting of two classes
Member variables are not written in parentheses
The declaration of the method is written in curly braces.
2>, watch out.
Member variables cannot be initialized in {} and cannot be accessed directly
Method cannot be called as a function
Member variables, methods can not be modified with static and other keywords, do not confuse with C language
The implementation of the class can be written after the main function, mainly after the declaration.
V. The difference between OC methods and functions 1. Common errors
The OC method can only be declared between @interface and @end and can only be implemented between @implementation and @end. This means that the OC method cannot exist independently of the class
C functions do not belong to the class, and the class is not associated with the C function only to define the function of the file all
C functions cannot access members of OC objects
ERR: A method has a declaration, but a function is written when implemented
2.OC Method Note
Method only declared, not implemented (classic error)
method is not declared, only implemented (compiler warning, but can invoke, OC's weak syntax)
Compile time: Access non-declared member variables directly error, access not declared method, just warning
OC Language-Grammar