First knowledge of Objective-c

Source: Internet
Author: User

First, learn about OC before

Introduction to OC Syntax

    • Keywords begin with @;
    • The string starts with @, such as NSLog (@ "I love iOS.");
    • Import can automatically prevent the contents of the file from being copied repeatedly;

If you are editing the OC program at the terminal, you must note the following compiled link directive:

    • Compiling cc–c main.m
    • Link cc main.o–framework Foundation
    • Run./a.out

The role of the Foundation framework:

    • Develop the necessary framework for OC, IOS, MAC programs
    • This framework contains a number of commonly used APIs (application programming interfaces)
    • The framework contains a lot of header files, if you want to use the contents of the entire framework, including its main header file can be

Two, a simple example of creating objects based on the OC class

1 #import<Foundation/Foundation.h>2 //definition of car class3 @interfaceCar:nsobject4 {5     @public6     intSpeed ;7     intWheels;8 9 }Ten- (void) run; One @end  A //The realization of car class - @implementationCar -  the- (void) run{ -NSLog (@"A car with%d wheels is runing"); - } - @end +  - intMain () + { ACar *p =[carNew];//the P pointer points to the new object created atP->speed = -; -P->wheels =4; -[P run];//sends a run message message mechanism to the car object that was created -}

Note The above procedures:

    1. Declare the function in the Declaration, in the implementation of the implementation of writing methods;
    2. Methods and functions are not a thing;
    3. The property value is initialized to 0 by default;
    4. Add "NSObject" after the class you created to allow car to have the ability to create objects;
    5. car* p =[car New] is a p pointer pointing to the in-memory Car object;
    6. [P run] sends the run message, the message mechanism;

The benefits of object-oriented encapsulation:

    • Closer to the way people think;
    • You just need to focus on the object and don't need to follow the steps.

First knowledge of Objective-c

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.