Objective-c Note One (HelloWorld)

Source: Internet
Author: User
As a fruit powder And programmer, running a strong interest in OC, began to learn IOS. I also want to work on IOS development in the future. And record your learning, as the saying goes, the program ape that will not summarize, not a good programmer!

 

 

Xcode can be downloaded in the AppStore

Open Xcode and point to the arrow:

Here we first select the Command Line Tool (console program) of the OS X Application:

Click Next:

Here we enter some information such as the project name, you can also just write:

Click Next

Then you will be asked to select the folder where the project is stored. Here I choose the desktop, and click Create:

 

Ok, although nothing has been done yet, HelloWorld is done.

Use the shortcut key command + R to run

Extension Meaning Extension Meaning
.c C language source file .mm Objective-C ++ source file
.cc, .cpp C ++ language source files .pl Perl source files
.h header file .o Object (compiled) file
.m Objective-C source files
#import <Foundation / Foundation.h>

This is a system file, that is, this file was not created by you. #import means to import or include the information of the file into the program, just like entering the content of the file here. You want to import the file Foundation.h because it contains information about other classes and functions.

 

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

Main is a special name used to accurately indicate where the program will start execution (should be the same as Java's main method). The reserved word int before main specifies that the value type returned by main is an integer.

 

@autoreleasepool {}

程序 Program statements in braces are executed in a context called "autoreleasepool". The mechanism of the automatic release pool is: it enables the application to effectively manage the memory used by the application when the application creates a new object.

 

NSLog is a function that only displays or records its parameters (or parameter list). But before it would show the execution date and time of the function, the program name, and other ...

Return 0; it means to terminate the execution of main and send back (or return) a status value of 0. According to the convention, 0 means that the program ends normally. Any non-zero value usually indicates that something went wrong.

Here is the following example: Create a class named Fraaction

#import <Foundation / Foundation.h>

@interface Fraction: NSObject

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numberator;
-(int) denominator;

@end
#import "Fraction.h"

@implementation Fraction
{
    int numberator;
    int denominator;
}
-(void) print {
    NSLog (@ "% i /% i", numberator, denominator);
}
-(void) setNumerator: (int) n {
    numberator = n;
}
-(void) setDenominator: (int) d {
    denominator = d;
}
-(int) numberator
{
    return numberator;
}
-(int) denominator
{
    return denominator;
}
@end
// ---- program part ----
#import <Foundation / Foundation.h>
int main (int argc, const char * argv [])
{
    @autoreleasepool {
        // Create a score instance alloc allocate space init initialization
        myFraction = [[Fraction alloc] init];
        // myFraction = [myFraction init];
        
        
        // Fraction * fact = [[Fraction alloc] init];
        
        // Set the score to 1/3
        [myFraction setNumerator: 1];
        [myFraction setDenominator: 3];
        
        // [fact setDenominator: 3];
        // [fact setNumerator: 7];
        
        // Use the print method to display the score
        NSLog (@ "The value of myFraction is:% i /% i", [myFraction numberator], [myFraction denominator]);
        // [myFraction print];
        //
        // NSLog (@ "Second fraction is:");
        // [fact print];
    }
    return 0;
}
I will introduce% i (similar to C # placeholder) in the future, which is the NSLog character used to get the int value.

The code I practiced involved the classes, objects, and methods of the next chapter. Because of the basics of java and C #, OC is almost the same to learn, but the syntax is different. Anyway, I love OC!
 

Objective-C Note 1 (HelloWorld)
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.