Declared property

A declared property provides a syntactical shorthand for declaring a class’s accessor methods and, optionally, implementing them. You can declare a property anywhere in the method declaration list, which is in the interface of a class, or in the declaration of a protocol or category. You use the following syntax:

@property (<#attributes#>) <#type#> <#name#>;

You begin a property declaration with the keyword @property. You can then optionally provide a parenthesized set of property attributes that define the storage semantics and other behaviors of the property. (Refer to the document that definitively describes property lists for descriptions of these attributes.)

Each property declaration ends with a type specification and a name. For example:

@property(copy) NSString *title;

This syntax is equivalent to declaring the following accessor methods:

- (NSString *)title;
- (void)setTitle:(NSString *)newTitle;

In addition to declaring the accessor methods, you can instruct the compiler to synthesize implementations of them (or inform the compiler that your class will synthesize them at runtime).

You use the @synthesize statement in a class’s implementation block to tell the compiler to create implementations that match the specification you gave in the property declaration.

@interface MyClass : NSObject
{
    NSString *title;
}
@property(copy) NSString *title;
@end
 
@implementation MyClass
@synthesize title;
@end

You use the @dynamic statement to tell the compiler to suppress a warning if it can’t find an implementation of accessor methods specified by an @property declaration.

@implementation MyClass
@dynamic title;
@end

Prerequisite Articles

Related Articles

    (None)