Objective-c @property and @synthesize

Source: Internet
Author: User

In Objective-c, you use @property to identify the attribute (typically an instance variable). Use the @synthesize to identify the variables declared in the implementation file, allowing the system to automatically generate the Set method and get method.

In other words, @property and @synthesize are paired, allowing the system to automatically generate the Setup method and get methods.

Cases:

Test.h

[CPP]View Plaincopy
    1. #import <Foundation/Foundation.h>
    2. @interface Test:nsobject {
    3. int IntX;
    4. int inty;
    5. }
    6. @property int intx,inty;
    7. -(void) print;
    8. @end


Test.m

[CPP]View Plaincopy
    1. #import "Test.h"
    2. @implementation Test
    3. @synthesize Intx,inty;
    4. -(void) Print {
    5. NSLog (@"intx+inty=%d", Intx+inty);
    6. }
    7. @end


Classtest.m

[CPP]View Plaincopy
  1. #import <Foundation/Foundation.h>
  2. #import "Test.h"
  3. int main ( int argc, const char* argv[]) {
  4. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
  5. Test *test = [[Test alloc]init];
  6. [Test setintx:1];
  7. [Test setinty:1];
  8. [Test print];
  9. [Test release];
  10. [Pool drain];
  11. return 0;
  12. }

Program Run Result:

    • Test.inty = 1;

      It is equivalent to:

      [CPP] view plaincopy
      1. [Test setintx:1];
      2. [Test setinty:1];
      At the same time, need to pay special attention: Copyright Notice: This article for Bo Master original article, without Bo Master permission not reproduced.

Objective-c @property and @synthesize

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.