IOS Dev (16) is one of the sections on some basic OC knowledge points.

Source: Internet
Author: User

IOS Dev (16) is one of the sections on some basic OC knowledge points.

  • Author: CSDN laruence
  • Blog: self in static methods of http://blog.csdn.net/prevention1

    Use self and self to represent this class in static methods (class methods. For example, the following two methods mean the same:

        + (void) wtf    {        [self alloc];    }    + (void) wtf    {        [ClassName alloc];    }
    The true meaning

    An object in OC is followed by a bunch of things after ".", indicating the get method or the set method, rather than the member variable. To get or set, it depends on whether the application scenario requires the return value or the void that does not need the return value. So there are:

    [[UIViewController alloc] init]; you can actually write it as UIViewController. alloc. init;
    3. Private Method

    The methods implemented by OC in. m, but not in. h, are private methods.

    4. Default scope of member variables

    Like C ++, there are three scopes:

    • Public
    • Protected
    • Private

      The member variables declared by OC in. h are protected by default. For example:

      @interface ClassName: NSObject{    int _age;    int _sex;}@end

      The above age and sex are both protected, that is, they can be accessed in the class and subclass.

      4. How do I specify the scope of member variables?

      Go directly to the Code:

      @interface ClassName: NSObject{    @public    int _age;    @private    int _sex;   }@end
      5. formal writing of get and set methods
      @interface ClassName: NSObject{    int _age;    int _sex;}- (int) age;- (void) setAge:(int)age;@end
      6. Write a constructor.

      There are two writing methods. Note that there is * In the first method, which indicates a pointer.

      - (ClassName *)initWithArg:(int)arg{}

      You can also use the id ~

      - (id)initWithArg:(int)arg{}
      7. Call the constructor of the parent class after inheritance
      - (id)initWithArg:(int)arg{    if (self = [super init])    {        _arg = arg;    }    return self;}
      8 simple Syntax of [[Blabla alloc] init]
      ClassName * cn = [[ClassName alloc] init]; ClassName * bn = [ClassName new]; // not recommended

      -

      Reprinted please indicate from: http://blog.csdn.net/prevention

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.