Basics of iOS Learning path--oc

Source: Internet
Author: User

    • Run process

1. Writing the OC Program:. m source File

2. Compile the. m file for the. O target file:cc-c xxxx.m

3. link. o File for a.out executable:cc xxxx.o-framework Foundation

4. Execute the a.out file:./a.out

    • #import function is just as good as # include, he avoids the multiple inclusion of the header file
    • To be able to use OC features, be sure to introduce #import <Foundation/Foundation.h>
    • Class definitions // @implementation and @end

//design (definition) of a car class//@implementation and @end//: nsobject: Let car this class has the ability to create objects (inheritance)@implementationCar:nsobject//This curly brace can only write all the attributes.{    //@public: Allows the object's properties to be accessed by the outside pointer, which is private by default    @public    intWheels//the initial value of the default base data type is 0}//in front of @end, curly braces {} write behavior outside//add a behavior to a Car object (method)//To add a behavior to an object, you must start with a minus sign, adding an object to the class is +. //the parentheses () in the OC method are just the main extension types- (void) run{//To access the properties inside the car object, you can use the property name directlyNSLog (@"%i a wheel,%f the speed of the car run up! ", wheels, Speed);}+(Void) test{//This is the class method. The above-number is the object method. }@end

    • Class calls
    // in OC, to perform some behavior, first write a [behavior performer's  behavior name]        //  New This behavior is completed, will return the address of this object    new  ];     // assign a value to the wheels property of the car object to which C points    4 // equivalent to (*c). Wheels = 4         // "Send" a run message to the object that the pointer variable C points to to let the object perform the run behavior    [C Run];
    • Declaration & Implementation
 //  declares a class    1. class Name 2. Inherited NSObject 3. Declaring a property 4. Declaring a method (just declaring, not implementing) 5. Member variables in the implementation and declaration cannot have the same name  */  @interface   book:nsobject{ Span style= "color: #0000ff;" > @public  double  Price; //     declares a method (behavior) -(void    @end  
// define (implement) a class /*  */@implementation-(void) reng{    NSLog (@ " %f's book was thrown away!  ", Price);}
    • Declaration and use of multiple parameters
- (void) fly{NSLog (@"I can fly, my age is%d", age);}//one parameter corresponds to a colon//the colon is also part of the method name- (void) Fly: (int) howheight{NSLog (@"I can fly, my age is%d, my height%d", age, Howheight);}//the case of multiple parameters. Withtime is part of the method name. Times is the parameter name- (void) Fly: (int) Howheight:withtime (int) times{}
    New ];    [P fly];    [P Fly:  About ];    [P Fly: Withtime: £ © ];
    • The difference between OC object methods and functions

1. The function belongs to the entire file, can be called anywhere in the file, the object's method belongs to only this object, only the object can call

2. The method of the object can only be declared between @infterface and @end, and the object method can only be implemented between @implementation and @end.

The Declaration and definition of a function can be written anywhere, and the function cannot be owned by a class and belongs to only one file.

    • Message mechanism

Sends a message to the object to which the pointer variable is pointing (the message name is the method name), and the object executes the appropriate method (dynamic)

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.