Objective-C attributes

Source: Internet
Author: User

Attribute Definition

@ Interface nothin: nsobject

@ Property nsstring * prop;

@ End

 

Access attributes

Nothin * nothin = [nothin new];

// Get

[Nothin prop];

// Set

[Nothin setprop: @ "nothin"];

After @ property is declared, the compiler automatically integrates the getter and setter attributes.

Attribute access method naming rules:

The getter method and attribute name are the same. For example, the getter method of prop is also named prop.

The setter method is an attribute name prefixed with "set" and capitalized with the first letter. For example, the setter method of prop is called setprop.

 

The default attribute is readwrite. Add readonly attribute: @ property (readonly) nsstring * prop to the attribute.

Set the custom access method: @ property (getter = getprop) nsstring * prop;

If multiple attributes are set at the same time, separate them with commas: @ property (ready, Getter = getprop) nsstring * prop;

 

Simple syntax for access attributes:

// Get

Nsstring * STR = nothin. firstname;

// Set

Nothin. firstname = @ "nothin ";

 

By default, the compiler automatically encapsulates an instance variable for the property. The instance variable is prefixed with an underscore in the property name. For example, the instance variable encapsulated by the prop property is _ prop.

You can directly access the instance variable within the class.

-(Void) Method

{

_ Prop = @ "nothin ";

}

You can also enable attributes to encapsulate custom instance variables by using the synthesize keyword in the. M file. For example:

@ Synthesize prop = custom_prop;

If @ synthesize is used but no instance variable name is specified for the attribute, the instance variable name encapsulated by this attribute is the same as the attribute name. For example

@ Synthesize prop; in this case, the instance variable name is also prop

 

If you do not want to use attributes to encapsulate instance variables, you can also define instance variables in. h or. M:

@ Interface nothin: nsobject

{

Nsstring * _ var;

}

 

 

 

 

 

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.