Basic syntax for OC (i)

Source: Internet
Author: User
Tags type null

1. Many languages are learned from HelloWorld, and OC's HelloWorld are as follows:

#import <Foundation/Foundation.h>int main (intconstChar *  Argv[]) {    @autoreleasepool {        //  OC version of HelloWorld        NSLog (@ "Hello, world! " );    }     return 0 ;}

2. Code Analysis

 1) header File

Compare # # Learning in C language

#include<stdio.h>file contains, copy the contents of the. h file to the current #include location#import<Foundation/Foundation.h>file contains, copy the contents of the. h file to the current#importLocation, foundation/Foundation.h This is a frame in OC/applications/xcode.app/contents/developer/platforms/macosx.platform/developer/sdks/macosx10.Ten. sdk/system/library/frameworks about the difference between include and import: #include""-user-defined file #include<>-System's Files#import<>-System's Files#import ""-the user's file Improt is an include upgrade, which automatically prevents duplicate inclusion, even if it is duplicated, and only once contains the function2Main entry function system call main entry function3) NSLog, output content to the console

Comparison of 3.C and OC

/*  1) source file compare C OC. h. h. m-oc source files. O. Out 2) data type, operator C        OC basic data type constructed type pointer type null type definition type BOOL new type, store Yes and NO ID Universal Pointer Boolean Boolean type, similar to bool, stored with true and False class class type Special     Selector for type SEL method nil empty (object is empty) blocks block Type 3) keywords, identifiers      C OC 32 keyword @ ...  Note: C keywords can be used in OC, OC keywords cannot use OC keywords in C, most of them are at the beginning of @ 4, statement control (branching and looping) C OC----------------->                    1) The branch if if else if else if else switch 2) loops---------------> while do and for                               Enhanced for loop (fast enumeration of OC) for in format: for (array type variable name in array) {       variable name;//direct use, variable each store value is not the same} 5, function definition contrast C                     OC return value function name (parameter) {functions in OC are no longer called "functions", called Methods in the body OC of the method function, are divided into two kinds: Object method, class method}                 Call Function name (argument) of the function;                       Invocation of the object method: the invocation of the [object name Method Name] class method: [Class name Method name] 6, new added two data types in OC logical type: BOOL Boolean C                      OC && BOOL | |            Boolean! BOOL stores the time Yes and no yes represents true actual value 1 No represents false actual value 0 Boolean stored when True and False true indicates true actual value 1 F   Alse represents false actual value 0*/#import<Foundation/Foundation.h>intMainintargcConst Char*argv[]) {@autoreleasepool {

//Nsarray *arr = @[@ "1", @ "2", @ "3"];// //For (NSString *str in arr) {// // //the contents of the output string//NSLog (@ "%@", str);// // } //1. Type BOOL//defining BOOL VariablesBOOL flag = YES;//The value of Yes is 1flag = NO;//the value of No is 0.Flag =3>3; printf ("Floag =%d\n", flag); //2. Boolean type (Boolean type)Boolean F1 =true;//true 1F1 =false;//false 0printf ("f1 =%d\n", F1); BOOL Flag1=YES; while(FLAG1) {flag=NO; } } return 0;}

Classes in 4.OC

/*  The definition of the first OC class: The definition of a class is divided into two parts: the implementation of class declaration Class 1) class declaration notation format: @interface class name: declaration of the parent class name {//member variable}          Declaration of Action (method) @end declaring a student class: @interface Student:nsobject {//attribute definition int age;          int Sno;      Float score;        }//Definition Behavior @end 2) class implementation format: @implementation class name @end; Implement a student class: Actually the method declared in the implementation class @implementation Student @end Note: 1) @interface, @implementation, @end an OC          Keyword 2) @end must not be missed write 3) the Declaration and implementation of the class should be paired to appear 3) object creation struct Student stu1;          Define a variable of type Student//Create a Student type Object object name is stu Student *stu = [Student new];  [Student new];         Call student's class method to do three things: 1) Allocate a chunk of memory to the heap of Memory 2) initialize the storage area (initialize each property of the class, base data type, initialize to 0,nsstring initialize to null)         3) Returns the first address of the memory space just allocated 4) Declaration and implementation of methods in the class ' in the declaration of the class in the @interface and @end directly, and in the property declaration of the {}, you can declare the method Method of declaration: OC has two methods: Object method and Class method                   Features of the object method: 1) The return value of the method is enclosed in parentheses by using the-Number 2) method name strict adherence to the identifier naming specification                   4) object idea, can only have object call method call format: [object name Method name];                      1) No Parameter Method-(return value type) method name;                     -(void) Qiaoke;                     -(void) playGame;                              -(void) tanlove;                            Implementation: Between @implementation and @end-(void) qiaoke{}                         2) method with parameters (1) An object method with a parameter-(return value type) XXX: (parameter type) formal parameter name;                          Note: Method name: "XXX:" Call: [object Name Method Name: actual argument];                                                  Implementation: Between @implementation and @end-(void) Eat: (NSString *) foodname{ } (2) Declaration of an object method with two or more arguments-(return value type) XXX: (parameter Type) parameter name andsss: (parameter type) parameter name; Method Name: Xxx:andsss://method with two parameters declared-(void) t                        Love: (NSString *) GF1 ANDGIRL2: (NSString *) GF2;     Call: [Stu tlove:@ "" andgirl2:@ ""];*/#import<Foundation/Foundation.h>//declaring a student class@interfacestudent:nsobject{//definition of a property (member variable/instance variable)    @public    intAge ; intSno; floatscore;}//Defining Behavior//Skipping class-(void) Qiaoke;//playing games-(void) PlayGame;//Fall in Love-(void) Tanlove;//declares a way to eat-(void) Eat: (NSString *) Foodname;//A method that declares two parameters-(void) Tlove: (NSString *) GF1 ANDGIRL2: (NSString *) Gf2;-(void) Tlove: (NSString *) GF1:(NSString *) Gf2;@end//implement a student class@implementationStudent//Skipping class-(void) qiaoke{NSLog (@"It 's skipping class! ");}//playing games-(void) playgame{NSLog (@"Are playing all landlords! ");}//Fall in Love-(void) tanlove{NSLog (@"I'm in love with Floret ! ");}//How to eat-(void) Eat: (NSString *) foodname{NSLog (@"Eating:%@", Foodname);}//A method that declares two parameters-(void) Tlove: (NSString *) GF1 ANDGIRL2: (NSString *) gf2{NSLog (@"and %@, and%@ also have a leg!\n", GF1,GF2);}-(void) Tlove: (NSString *) GF1:(NSString *) gf2{NSLog (@"and %@, and%@ also have a leg!\n", GF1,GF2);}@endintMainintargcConst Char*argv[]) {@autoreleasepool {//create an object for a student classStudent *stu = [StudentNew]; NSLog (@"age:%d,sno:%d,%.2f",stu->age,stu->sno,stu->score); //Object STU member variable assignment//A member variable of a class cannot be accessed by default in OC outside of the class//to force access to member variables of a class with @public//assigning values through object StuStu->age = at; Stu->sno = the; Stu->score =56.78; NSLog (@"age:%d,sno:%d,%.2f",stu->age,stu->sno,stu->score); //methods for invoking objects with objects[Stu Qiaoke];        [Stu PlayGame];        [Stu Tanlove]; [Stu Eat:@"stir-fried crisp corner"]; //methods that call two parameters[Stu Tlove:@"Feng"ANDGIRL2:@"Hibiscus"]; [Stu Tlove:@"Feng":@"Hibiscus"]; }    return 0;}

Basic syntax for OC (i)

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.