Objective-C Properties, objective-c

Source: Internet
Author: User

Objective-C Properties, objective-c

Objective-C Properties

Apple introduced properties, a combination of new compiler ctictives
And a new attribute accessor syntax.

Apple introduced the attribute concept. New compiler commands and new attribute access syntaxes.

1.1 Shrinking the Interface reduces interface code

 

@ Property float rainHandling; says that objects of the class AllWeatherRadial have an attribute, of type float, called rainHandling. it also says that you can set the property by calling-setRainHanding: and that you can access the attribute by calling-rainHandling.

@ Property float rainHandling; this indicates that this object has a class attribute of the float type and its name is rainHandling. Similarly, you can set the property value by calling setRainHanding.

1.2 Shrinking the Implementation

@ Synthesize rainHandling;

@ Synthesize snowHandling;

@ Synthesize is a compiler feature that says "create the accessors for this attribute ."

@ Synthesize is a characteristic of the compiler. It indicates that this attribute is obtained.

 

@ Synthesize is not code generation. You won't ever see the code that implements-setRainHandling: and-rainHandling, but these methods will exist and will be callable.

@ Synthesize is not a code generator. However, this method exists and can be called.

If you provide the methods in your class for the property, the compiler doesn't create them. The compiler only creates methods that are missing.

If you provide a method for this attribute, compiler will not create these methods. Compiler

Create only the missing methods.

Most properties are backed by a variable, so when you synthesize the getter and setter, the compiler automatically creates an instance variable with the same name as the property.

Most properties return a variable, so when you synchronize the getter and setter methods, compiler automatically creates an instance variable with the same name as this property.

Notice that the header file has two variables called rainHandling and snowHandling. The setter and getter will use these variables. If you don't declare those variables, the compiler creates them.

Note that the header file has two variables. If not written, compiler will automatically add these two methods.

1.3 Dot notation

The dot notation looks a lot like structure access in C and object access in Java-on purpose.

Point operators want to store in C and Object Access in java.

1.4 Objecting to Properties object for Properties

Objects bring some added complications.

Objects make things troublesome.

Recall that we retain and release objects as they flow through our accessors.

When they are going through the accessor, we retain and release pair.

For some object values, fig string values, you want to always-copy them. yet for other object values, like delegates (which we'll talk about in the next chapter), you don't want to retain them at all.

For a string, you want to copy it, but for a delegate, you don't want to retain them.

@ Property (copy) NSString * name;

@ Property (retain) Engine * engine;

 

 

You can use some other decorations, like nonatomic, which makes accessors a bit faster if they won't be used in a multithreaded environment.

If they do not need to be in a multi-threaded environment, nonatomic makes the accessor faster,

 

S. You can also use assign if you don't want the attribute object to be retained, to help avoid retain cycles.

If you do not want attribute objects to be retained, we will try to avoid retian loops.

You can only specify retain and copy attributes for retainable pointers (I. e. objective-C objects ). all other types, such as C and nonretainable pointers, must use assign and manage memory manually.

 

If you provide either or both the setter and getter yourself, you cannot use atomic attribute; you must use nonatomic.

If you write any of the setter or getter methods, you cannot use the atomic attribute. You must use the nontomic attribute.

 

1.5 Appellation spring alias

The name of the property has been the same as the name of an instance variable that backs that property.

Generally, the property name should be the same as the instance variable name of the returned property.

Sometimes, though, you may want one name for the instance variable and another for the public attribute name.

However, sometimes you want to name an instance variable, while the Public attribute name is another.

@ Interface Car: NSObject

{

NSString * appellation;

NSMutableArray * tires;

Engine * engine;

}

@ Property (copy) NSString * name;

@ Property (retain) Engine * engine;

And then change the synthesize directive: @ synthesize name = appellation;

 

In init, change

Name = @ "Car ";

To

Self. name = @ "Car ";

 

 

 

What's that self-dot-name business? It's a bit of disambiguation to let the compiler know that we want to vector through the accessors. if we just use a naked name, the compiler assumes that we're re directly modifying an instance variable. to go through the accessors, we can write [self setName: @ "Car"]. remember that the dot is just shorthand for making this exact same call, so self. name = @ "Car" is just another way of saying the same thing.

 

Only to remove ambiguity.

 

1.6 Read-Only About It Read-Only

You might have an object with an attribute that is read-only.

There are some attributes that you can only read.

You can code for these situations with more attributes on @ property.

You can add attribute in @ property

 

By default, properties are mutable: you can read and write them. Properties have a readwrite attribute you can use for specifying this.

By default, properties are variable. You can read and write them.

@ Property (readonly) float shoeSize;

@ Property (readonly) NSString * licenseNumber;

When the compiler sees that @ property is readonly, it generates a getter but not a setter for that attribute.

When compiler sees @ property read-only, it should generate only the getter method instead of the setter method.

1.7

What if you 'd rather not have a variable, getter, and setter for your property?

What if you do not want the getter and setter methods?

You can use the keyword @ dynamic to tell the compiler not to generate any code or create a variable for the property.

You can use @ dynamic to tell the compiler not to generate any code.

@ Property (readonly) float bodyMassIndex;

@ Dynamic bodyMassIndex;

-(Float) bodyMassIndex

{

/// Compute and return bodyMassIndex

}

1.8 chang the method name

You might not like the names of the methods that are generated by default. They're in the form of blah and setBlah :.

Sometimes you want to use your own method name.

To overcome this, you can specify the names of the getter and setter that the compiler generates. Use the attributes getter = and setter = to define the preferred names of your methods.

Therefore, you should set getter = and setter attribute in @ property.

@ Property (getter = isHidden) BOOL hidden;

1.9 properties is not omnipotent

-(Void) setTire: (Tire *) tire atIndex: (int) index;

-(Tire *) tireAtIndex: (int) index;

These methods don't fit into the fairly narrow range of methods that properties cover. properties will let you replace only-setBlah and-blah methods, but not methods that take extra arguments, like the tire's position on the car.

The above method is not suitable for properties, because the property only allows you to replace the part of the setter and getter methods without parameters.

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.