Objective-c Properties

Source: Internet
Author: User

Objective-c Properties

Apple introduced properties, a combination of new compiler directives
and a new attribute accessor syntax.

Apple introduced the concept of attributes. New compiler commands and new attribute access syntax.

1.1 Shrinking The Interface reduced Interface code

?

@property float rainhandling; says that objects of the class allweatherradial has an attribute, of type float, called rainhandling. It also says that's can set the property by Calling–setrainhanding:and so can access the attribute by Calling-r Ainhandling.

@property float rainhandling; This indicates that the object has a class property, a float type, and a name of rainhandling. Again, 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 compiler feature that says to create this property for getting.?

?

@synthesize is not code generation. You won ' t ever see the code that implements–setrainhandling:and–rainhandling, but these methods would exist and would 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 is missing.

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

Only missing methods are created.

Most properties is backed by a variable, if 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 Getter and setter methods, compiler automatically creates an instance variable with the same name as the property.

Notice the header file has a variables called rainhandling and snowhandling. The setter and getter would use these variables. If you don ' t declare those variables, the compiler creates them.

Note that the header file has two variables, and if not, compiler will automatically add both methods.

1.3 Dot notation?

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

The dot operator wants to store in C as well as object access in Java

1.4 Objecting to properties object for property

Objects bring some added complications.

Object makes things difficult.

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

When they go through accessor, we retain and release to sex.

For some object values, particularly 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.

Like string you want to copy it, and for 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 is used in a multithr eaded environment.

If they are not in a multithreaded environment, nonatomic makes accessor faster,

?

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

If you do not want the Property object to be retained, then we will try to avoid retian loops.

You can have 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 getters yourself, you cannot use atomic attribute; You must use Nonatomic.

If you write any of the setter or getter methods yourself, you cannot use the Atomic property, you must use the Nontomic property.

?

1.5 appellation Spring? Alias

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

In general, the name of the property should be the same as the name of the instance variable that returns the property.

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

But sometimes you want the instance variable to have a name, and the public property name is another.

@interface Car:nsobject

{

? NSString *appellation;

? Nsmutablearray *tires;

? Engine *engine;

}

@property (copy) NSString *name;

@property (retain) Engine *engine;

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

?

In Init, change

Name = @ "Car";

To

Self.name = @ "Car";

?

?

?

What's the Self-dot-name business? It's a bit of disambiguation to let the compiler know, we want to vector through the accessors. If We just use a naked name, the compiler assumes that we ' re directly modifying an instance variable. To go through the accessors, we can write [self setname:@ "Car"]. Remember that dot was just shorthand for making this exact same call, so self.name = @ "Car" is just another it's sayi ng the same thing.

?

Just to get rid of ambiguity.

?

1.6 Read-only about it read only?

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

There are some properties you allow it to read only.

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

You can add attribute to the @property.

?

By default, properties is mutable:you can read and write them. Properties has a ReadWrite attribute you can use for specifying this.

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

@property (readonly) float shoesize;

@property (readonly) NSString *licensenumber;

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

When compiler sees @property read-only, it should only produce getter methods, not setter methods.

1.7?

What if you ' d rather has a variable, getter, and setter for the Your property?

What if you don't 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 the key @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 the names of the methods is 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 getters and setter that the compiler generates. Use the attributes getter= and setter= to define the preferred names of your methods.

To do this, you should set the 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 would let you replace Only–setblah And–blah methods and not methods so take extra arguments and like the tire ' s position on the car.

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

Objective-c Properties

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.