UnderstandingObjective-CThe features and features are described in this article. Currently, only AppleObjective-CAs a supported language. Let's just look at the details. The differences with C ++ are:
All classes in Objective-C must inherit from NSObject.
All objects in Objective-C are pointer-based.
Objective-C replaces this with self.
Objective-C uses id instead of void *.
Objective-C uses nil to indicate NULL
Objective-Ck only supports single inheritance.
Objective-C uses YES/no to indicate true/FALSE
Objective-C Use # import instead # include
In Objective-C, messages are used to represent class methods and the [aInstance method: argv] Call form is used.
Objective-C supports reflection mechanism
Objective-C supports Dynamic Typing, Dynamic Binding, and Dynamic Loading.
The similarities with C ++ are:
The shared part is the same as that shared by C.
You can use assert (BOOL), which is generally replaced by NSCParameterAssert (BOOL.
Description of naming prefixes in Objective-C:
- NS-:NextStep
- CF-:Core Foundation
- CA-:Core Animation
- CG-:Core Graphics
- UI-:User Interface
Message particularity in Objective-C:
CallMessageClass does not know how to respond to thisMessage. If it does not know how to process the message, it automatically transfers the message to another class, such as its parent class.
The class that calls the message can be nil. In C ++, before using class methods, we need to check whether the object is empty. Therefore, when implementing the destructor, we usually have the following code, for example, if (var) {delete var;}, but in Objective-C, we can directly write [var release]; even if var = nil, there will be no problem.
The function declaration formats in Objective-C include:
- -/+ (return type) function_name;
- -/+ (return type) function_name : (parameter type) parameter;
- -/+ (return type) function_name :
- (parameter type) parameter1 otherParameter :
- (parameter_type) parameter2
The preceding parameter description:-indicates a general function, and + indicates a static function. OtherParameter is the parameter alias (the alias of the first parameter is omitted ).FunctionIt is convenient to specify when calling.
Constructor/destructor in Objective-C
In Objective-C, init ()/release () corresponds to the constructor/destructor of C ++. Alloc ()/dealloc () corresponds to the new and delete functions of C ++. dealloc () does not need to be called manually because of the automatic call of reference count.
Init ()/release () of the parent class in Objective-C ()FunctionManual call of subclass is required. And must be called every time. Different from automatic calls of C ++.
The constructor (-(id) init) calls the form CSample * pSample = [CSample alloc] init]. Among them, alloc (+ (id) alloc) is the inherited static function, init is inherited.FunctionFor example, when a general function is rewritten, it is equivalent to overwriting (without parameters) or overloading (with parameters) of C ++ ).
The Destructor (-(void) release reduces the reference count by 1. When the value is 0, the release () of the parent class automatically calls dealloc (-(void) dealloc );
If Objective-C does not have a data member, you can omit {}. We recommend that you keep it.
The inherited method, for example,-(id) init can be omitted in the header file. We recommend that you retain
In 0-C, There is only access restriction for data members, and there is no access restriction for methods.
Like C ++, data members have three access restrictions: public, protected, and private. The default value is protected.
Example:
- @interface AccessExample: NSObject {
- @public
- int publicVar;
- @protected
- int protectedVar;
- @private
- int privateVar;
- }
- @end
Method access restrictions can be implemented through Category
Example:
- @interface MyClass
- - (void) sayHello {
- NSLog(@"Hello");
- }
- @end
-
- @interface MyClass(Private)
- - (void) kissGoodbye;
- @end
Objective-C does not have static variables of the class, but only global variables.
Arrays NSArray in Objective-C can save different types of data.
Objective-C also supports class type check during run-time.
- - (BOOL) isKindOfClass: classObj
Used to determine whether the object belongs to a class or its subclass
- - (BOOL) isMemberOfClass: classObj
Used to determine whether the object belongs to a class and does not include subclasses)
- - (BOOL) respondsToSelector: selector
Used to determine whether the object can respond to a message. Here, we can understand the parameters following @ selector as function pointers in C ++.
Note:
1) Don't forget @
2) @ selector is followed by () instead of [].
3) The message name must be followed by: whether or not the message contains parameters. For example:
- [pSquare respondsToSelector:@selector(Set: andHeight:)]。
- + (BOOL) instancesRespondToSelector: selector
Used to determine whether the class can respond to a message. This is a staticFunction.
-(Id) specify mselector: selector: Call the selector method of the object.
Similar to respondsToSelector, conformsToProtocol is used to dynamically check whether an object complies with a certain protocol.
Category: adds some new functions to an existing class without source code.
Only new methods can be added. New data members cannot be added.
The Category name must be unique.
Protocol: equivalent to pure virtual class in C ++
Shape:
- @interface MyDate: NSObject <Printing> { } @end
Usage:
- MyDate * dat = [[MyDate alloc] init]; id<Printing> var = dat; [var print]。
Note: We first declare the Printing Protocol. Any class that complies with this Protocol must implement the print method. In Objective-C, we use <> to indicate compliance with an agreement. When a class declaration complies with a protocol, it must implement all methods in the Protocol in the. m file. Use id <Printing> as the type, rather than the Printing * var in C ++.
IBOutlet and IBAction: These two things do not actually play a major role in syntax. If you want to see the control object in Interface Builder, add IBOutlet before the definition to see the outlet of this object in IB, if you want to control an object to execute certain actions in Interface Builder, add (IBAction) before the method ).
Avoid nesting more than two layers in a row of statements.
Message forwarding:
- - (void) forwardInvocation: (NSInvocation*)anInvocation;
Summary: UnderstandingObjective-CThe features and features are described. I hope this article will help you!