OC, Swift property, member variable

Source: Internet
Author: User

First, @property

Using @property-Modified variables, the compiler automatically generates an underlined property with the same name and a corresponding get set method. Pass. The way Access property is actually access to the corresponding get set method, so in get set can not use self. The form of the property, otherwise you will fall into a dead loop. When simultaneously overriding two methods is required in the implementation file to add @syntheSize property = _property;

In a class, you can directly access its corresponding property.

A.h@property (nonautomic,copy) nsstring *a;//a.m@synthesize a = _a; -(void) SetA (NSString *val) {     _a = [NSString stringWithFormat: @ "%@123", Val];  } -(NSString *) a{    return _a;      }

  

In OC, only the attributes declared in the. h can be accessed externally, and the same modifier in Swift controls the access level of the member variable.

In Swift, the attributes are divided into the store and computed property, with a set get willset didset four methods.

The Store property uses the default Get set method, and once the Get or set method is overridden, it is determined that the variable must be a computed property.

Didset and Willset can only be used to store properties.

As with OC, use self in get . gets the property value or use self in set, Willset, Didset . setting the value of a property causes an infinite loop to crash.

OC, Swift property, member variable

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.