An analysis of the program structure of OBJECTIVE-C and the Object-oriented programming method _ios

Source: Internet
Author: User
Tags instance method



The structure of the OBJECTIVE-C program
just like learning all programming languages, the first program is to print "Hello world!" on the screen. ”。





I read the book to use the compiler is still relatively old, I now use Xcode4.2.1, it has automatic memory management, so some of the books on the program may be an error. You can resolve this problem by not selecting the Use Automatic Reference counting option when you create the project.





Run Xcode, create a new command line tool project, named Firstprogram.











In the next step, we also cancel the use Automatic Reference counting option.





Regardless of what the auto-generated code is, we change the code to the following:



Copy Code code as follows:

The Example 
#import <Foundation/Foundation.h> 
int main (int argc, const char * argv[]) { 
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
NSLog (@ "Hello, world!"); 
[Pool drain]; 
return 0; 


Run, as shown below:









Here's a brief description of the code:





1, the first line of the program is a comment, like C/c++/java, objective-c annotation can also be used///////////?? * * to achieve.





2, #import <Foundation/Foundation.h>





Tells the compiler to locate and process the file named Foundation.





3, int main (int argc, const char * argv[])





Specifies that the program name is main, which is a special name indicating where the program started.





4, NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];





Space is reserved in memory for the automatic free pool.





5, NSLog (@ "Hello, world!");





Specifies that you want to invoke a routine named NSLog. The argument passed to NSLog is the string @ "Hello, world!", which is a NSString object.





6, [pool drain];





Used to free allocated memory pools and the objects associated with the program.





Classes, objects, and methods
1. Examples and methods





(1) Use the class to create an instance:



Copy Code code as follows:

Yourcar = [car new]; 

Here the car is a class, Yourcar is an object








(2) Apply methods to classes and instances:



Copy Code code as follows:

[Classorinstance methed]; 

The name of the class or instance should be followed by "[", "];" Used to terminate. This statement corresponds to the name of the object in Java. method name








Another example:



Copy Code code as follows:

Currentmileage = [Yourcar currentodometer]; 

Where Currentmileage is used to receive return values








2, a section of detailed code and description:



Copy Code code as follows:

#import <Foundation/Foundation.h>


@interface section
@interface Fraction:nsobject {
int numerator;
int denominator;
}
-(void) print;
-(void) Setnumerator: (int) n;
-(void) Setdenominator: (int) D;
@end


Copy Code code as follows:

//@implementation section
@implementation Fraction
-(void) Print {
NSLog (@ "%i/%i", numerator,denominator);
}
-(void) Setnumerator: (int) n {
Numerator = n;
}
-(void) Setdenominator: (int) d {
denominator = D;
}
@end

Copy Code code as follows:

Program section 
int main (int argc, const char * argv[]) 

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
Fraction *myfraction; 
myfraction = [Fraction alloc]; 
Myfraction = [myfraction init]; 

[Myfraction setnumerator:1]; 
[Myfraction Setdenominator:3]; 

NSLog (@ "The value of Myfraction is:"); 
[Myfraction print]; 
[Mufraction release]; 
[Pool drain]; 
return 0; 


(1) Line 4th is a method of declaring a class, @interface the new class name: The parent class name








(2) Line 8th to 10th defines three methods, where "-" represents the instance method, and "+" represents the class method.





An instance method can always access its instance variable, but the class method does not, because the class method only handles the class itself, not any instances of the class





(3) The 31st to 32nd Line can be merged into



Copy Code code as follows:

Myfraction = [[Fraction alloc] init]; 

Or
Copy Code code as follows:

myfraction = [fraction new]; 

(4) line 39th is used to free memory for object myfraction, it is noteworthy that the latest Xcode compiler can implement automatic free memory










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.