Objective-C grammar learning essential documents for beginners

Source: Internet
Author: User

Objective-CGrammar learning is the content to be introduced in this article. It mainly introducesVariable,Method,Object. Let's take a look at the details.

Variable definition

Interface file *. h). The definition should be placed in brackets after the interface name.

ObjectTo use the pointer [NSButton * aButton;], use the normal name

NormalVariableNormal Definition

Method Definition

Defined in interface file *. h). The definition is placed out of brackets and marked before @ end.

Start with the symbol [-] Table object method, [+] Table static method), followed by the return value type enclosed in parentheses, followed by the method name, and add parameters starting with a colon, parameters are in the normal definition format, but the type should be enclosed in parentheses. The parameters after the first parameter should be marked with a semicolon to end [-(void) testFunction :( NSString *) aString secondPara :( int) aInt;]

Method name starts with a lowercase letter

Method implementation

Implementation file *. m). import the interface file [# import "MyClass. h "], the method is defined between the @ implementation and @ end keywords, similar to the definition of the method in the interface, but the implementation of the method is between braces

The parameters of the method are part of the method. If the parameter is a pointer, the memory needs to be re-allocated during use, and the memory needs to be released after use. If the parameter is a common variable, it is used normally, for example:

 
 
  1. -(void)setName:(NSString *)tmpName{  
  2.     [tmpName retain];  
  3.     [tmpName release];  
  4.     tmpName = name;  

After calling tmpName, the memory release operation calls a release method.

The object of the initialization class can be implemented in method [-(id) init.

Use of methods and variables

Instantiate an object: first allocate memory and create a class

 
 
  1. {MyClass *anInstance=[MyClass alloc;]} 

Then initialize the object {[anInstance init]} (the init method of the instance is called), and the instance is generally instantiated:

 
 
  1. {MyClass *anInstance =[[MyClass alloc] init];} 

Use brackets to call {[object message]} and end with a semicolon

After the object operation, add {[object method: para1 labelPara2: para2]} to the parameter. The method can be nested and called in the same way as the parameter to be used.

Call other methods in the same class. The object name is self

You can use the instance to directly call the object's variables, but it is best to access the variables through methods.

The returned value can be used normally.

Of course, you must release the instance created with alloc:

 
 
  1. {[anInstance release];} 

Automatic Memory release Mode

Replacing alloc with stringWithSting can enable the system to automatically release the memory.

Creation object:

 
 
  1. +(id)stringWithString:(NSString *)aString;  
  2. *aString =[NSString stringWithString:@"Hello,World!"] 

Set automatic recovery when instantiating objects:

 
 
  1. {MyClass *anInstance=[[[MyClass alloc] init] autorelease];} 

The general language uses the keyword class to define the class, while the objective-c uses the keyword @ interface and @ end to declare, And the keyword @ implementation plus @ end is used to define the class. Second, when the class object is used to call the method of this class, the general advanced language is usedObjectThe object name and the called method name are enclosed in brackets in objective-c.

(1) When the Objective-C compiler processes files with a suffix of m, it can identify the code of Objective-C and c. When processing mm files, it can recognize Objective-C, c, c ++ code, but the cpp file must only use c/c ++ code, and in the header file of the cpp File include,
The Objective-C code cannot appear either, because cpp is only cpp.

(2) Mix cpp in the mm file and use it directly. Therefore, Objective-C Mixed cpp is not a problem.

(3) Mixing Objective-C in cpp is actually what we want to write modules using Objective-C.

If the module is implemented by class, write the class definition according to the cpp class standard. The header file cannot contain Objective-C, including # import cocoa. Objective-C can be used in implementation files, that is, class implementation code,
Yes, but the suffix is mm. If the module is implemented by functions, the header file must declare the function in the c format. In the implementation file, the c ++ function can use Objective-C, but the suffix is still mm or m.

Summary: as long as the cpp file and the cpp include file do not contain Objective-C, you can use it. The key to mixing cpp with Objective-C is to use interfaces, instead, you cannot directly use the implementation code. In fact, cpp is a mixObjective-CCompiled o files,
This is actually a different thing, so it can be used. WhileObjective-CIt is easy to mix cpp and use it directly, becauseObjective-CCompiler support

Summary:Objective-CThe content of the essential documents for beginners of grammar learning has been introduced. I hope this article will help you!

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.