Objective-c Study Notes-Part 5-declarative attributes

Source: Internet
Author: User

This section discusses how to use declarative attributes and the automatically generated method inside the system as the property access interface, and the instance variable Management introduced in this section.

The declarative properties declared properties can be used to conveniently implement the accessors method accessor methods.

Declarative attributes provide a clear and clear specification for the visitor's method behavior.

The compiler helps you extend the application attributes to the accessors to reduce the numberCodeQuantity.

Declarative attributes are also a type of identifier and have their own scopes, so the compiler can detect undeclarative attributes.

Syntax:
@ Property (attributes) type name;

For example:
@ Interface myclass: nsobject
{
Float value;
}
@ Property float value;
@ End

Setter semantics:
Assign: simple assignment
Retain: the previous value receives a release message.
Copy: Copy

If you do not use the Garbage Collector, you must specify assign/retain/copy. Otherwise, you will receive a warning.

Atomicity and non-atomicity can be specified.

If it is retain/copy, an internal lock is automatically used to implement atomicity when automic is not specified.

If you use the garbage collection mechanism, you can specify whether the reference is a strong reference or a weak reference, but this is informal.

@ Synthesize firstname, lastname, age = yearsold;

Attribute declaration with instance variable name specified.

Whether or not you specify the instance variable name, this variable is only valid for the current class and not visible to the parent class.

@ Dynamic

About using @ Properties

Any object-C class can be declared.

You can declare it again in the subclass, and Modifier

Copy:
This property is useful in some situations. For example, you need to assign a mutable string to ensure that subsequent modifications do not affect the variable that has been assigned a value.

Non-asign property should
-(Void) dealloc {
[Property release];
[Super dealloc];
}
Or (new version, because instance variables cannot be directly accessed)
-(Void) dealloc {
[Self setproperty: Nil];
[Super dealloc];
}

The property in the subclass can use writable to cover the readonly attribute in the parent class.

The impact of different attributes on performance.

In the old environment, the @ synthesize attribute needs to manually declare the instance variable, but not in the new system. The system will automatically help you.

This section involves different types of attributes and the resulting differences in the value assignment mode, runtime performance, Operation atomicity, thread security, backward compatibility, and other issues. You need to take a closer look at these issues.

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.