Objective-C is not as difficult as it looks

Source: Internet
Author: User

I have seen many development languages, including C, C ++, Java, Python, Ruby, Lua, and PHP, basically, when I first read a new language, I can quickly guess the syntax characteristics of the language. However, when I first read objective-C, I was completely shocked, there will be a feeling of flowers in the fog!

However, after reading some documents, I feel that objective-C is not as horrible as it looks, so it is unattainable. Here I will briefly summarize the features of objective-C.


Objective-c features


  • Objective-C is the superset of C. That is to say, all objective-C in C has. Objective-C has more Oo (Object-Oriented) features than C does.
  • The default objective-C extension is. M. The header file extension is the same as that of C. H.
  • In objective-C, we recommend that you use the following methods to import header files: # Import The advantage is that the same head file is introduced only once, instead of unnecessary code.
  • Objective-C:
    @interface Person { 
        int age; 
        int hight; 

    -(int) func1; 
    -(void) func2: (int) n; 
    @end

    Between @ interface and @ end is the class definition, and between @ interface, {...} defines the class member variables,
    The following defines the method of the class: (Return Value Type) method name: (parameter type) parameter

  • Objective-C implementation class method:
    @implementation Person 
    -(void) func1 { 
        printf( "%i/%i", age, hight ); 

    -(void) func2: (int) n { 
        age = n; 

    @end

    @ Implementation and @ end are class implementations.

  • Class instantiation:
    Person *ps = [[Person alloc] init];
    Here [cannot] instantiate a person variable like C ++: person pS. In addition, objective-C uses pointers to operate on objects.
  • Class method call:
    [ps func1]; 
    [ps func2: 10]

    The former is a method call without parameters, and the latter is a method call with parameters.
  • Definitions and calls of Multiple Parameter methods:
    -(Void) func2: (INT) n label2: (INT) m label3: (INT) P; // put this sentence in @ interface @ end class method definition

    // Place the following code between @ implementation @ end.
    -(Void) func2: (INT) n label2: (INT) m label3: (INT) P {
    Age = N;
    Height = m;
    Th = P; // The third parameter is used only to express multiple parameters, which is inconsistent with the first example.
    }

    // The Call Code is as follows:
    [PS func2: 53 label2: 89 label3: 982];

    A lot of people may think this is weird. Why are there tags in the parameters next to them, but the first one is not.
    First, let's talk about the label (like label2, label3 above): the label is a tag used to determine the purpose of the corresponding parameter, that is, if you are not in the mood, you can name it casually, of course, it is recommended that the tag name be used to reflect the purpose of this parameter, or the purpose of this method when these parameters are currently used.
    Why is there no tag for the first parameter? In fact, you can regard the first method name as the tag of the first parameter, so that each parameter has a tag, and then a string of parameters are sent to the object as a [Signal.
    The code is organized as follows for the sake of beauty:
    [ps func2: 53  
        Label2: 89  
        Label3: 982];

    Such a tag corresponds to a parameter, which is very intuitive.

  • Access permission: Objective-C also has three access permissions: Public Private protect, and the default permission is protected.
    The public keyword modifies resources and can be accessed anywhere.
    The protected keyword modifies resources and can be accessed in the class and its subclass.
    Private keywords modify resources and can only be accessed within the class.
  • Public Member method of the external response class:
    A-> publicvar = 5; // where A is an object publicvar is a public Member
  • Because the member variables in objective-c use protected by default (if declared as public is not safe), that is, this class, and can be accessed in sub-classes, and external access is not allowed, so if you want external access or change the value of the member variable, You need to define some methods such as setxxx getxxx (by default in the O-C, there is no get, but directly XXX. Setxxx is used to set the value of the member variable, and the xxx method is used to obtain the value of the member variable. The O-C introduces two keywords @ property and @ synthesize to simplify the operation. You don't need to write a bunch of complicated code.
    // When defining
    @ Interface fraction: nsobject {
    Int numerator;
    Int denominator;
    }

    -(Void) print;

    //-(Void) setdenominator: (INT) D;
    //-(INT) denominator;
    @ Property (retain) int denominator; // equals to the comment removed from the above two sentences

    //-(Void) setnumerator: (INT) N;
    //-(INT) numerator;
    @ Property (retain) int numerator; // equals to the comment removed from the above two sentences
    @ End

    // Implementation
    @ Implementation

    @synthesize denominator; 
    @synthesize numerator; 

    @end

  • The syntax for declaring property is:
    @ Property (parameter) type name;

    There are three types:
    1. read/write attributes (readwrite/readonly): Needless to say as the name implies
    2. Set attributes (assign/retain/copy): Assign is a direct value, which is suitable for numeric values. Retain is the most common type. Generally, objects are retained. copy is a copy, string.
    3. atomicity (nonatomic): The default value is atomic. To ensure the consistency of a member variable, a multi-threaded program must use atomic.

  • Nil In the O-C is actually an object of the ID type, so you can pass information to nil, such as [nil message1].
  • The bool data in the O-C includes Yes and no, not true and false.
  • The protocol in the O-C is like an interface in C ++.
    // Define the definition protocol
    @ Protocol Printing
    -(Void) print;
    @ End

    // Introduce the Protocol
    @ Interface fraction: nsobject <printing, nscopying> {
    Int numerator;
    Int denominator;
    }

    -(Fraction *) initwithnumerator: (INT) n denominator: (INT) D;
    -(Void) setnumerator: (INT) D;
    -(Void) setdenominator: (INT) D;
    -(Void) setnumerator: (INT) n anddenominator: (INT) D;
    -(INT) numerator;
    -(INT) denominator;
    @ End

    // In the main file, you only need to implement the print method.

  • The O-C has no way to overload over loading
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.