Understanding Objective-C features and features

Source: Internet
Author: User

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:

 
 
  1. NS-:NextStep   
  2. CF-:Core Foundation   
  3. CA-:Core Animation   
  4. CG-:Core Graphics   
  5. 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:

 
 
  1. -/+ (return type) function_name;   
  2. -/+ (return type) function_name : (parameter type) parameter;   
  3. -/+ (return type) function_name :   
  4. (parameter type) parameter1 otherParameter :   
  5. (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:

 
 
  1.  @interface AccessExample: NSObject {  
  2. @public  
  3.     int publicVar;  
  4. @protected  
  5.     int protectedVar;  
  6. @private  
  7.     int privateVar;  
  8. }  
  9. @end  

Method access restrictions can be implemented through Category

Example:

 
 
  1. @interface MyClass  
  2. - (void) sayHello {  
  3.     NSLog(@"Hello");  
  4. }  
  5. @end  
  6.     
  7. @interface MyClass(Private)  
  8. - (void) kissGoodbye;  
  9. @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.

 
 
  1. - (BOOL) isKindOfClass: classObj 

Used to determine whether the object belongs to a class or its subclass

 
 
  1. - (BOOL) isMemberOfClass: classObj 

Used to determine whether the object belongs to a class and does not include subclasses)

 
 
  1. - (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:

 
 
  1. [pSquare respondsToSelector:@selector(Set: andHeight:)]。   
  2. + (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:

 
 
  1. @interface MyDate: NSObject <Printing> { } @end  

Usage:

 
 
  1. 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:

 
 
  1. - (void) forwardInvocation: (NSInvocation*)anInvocation;  

Summary: UnderstandingObjective-CThe features and features are described. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.