you should know what is the use of @property.
- @property offers a way to define the information that a class is intended to encapsulate. If you declare an object/variable using @property, then that object/variable will be accessible to other classes importing its class.
If you declare an object using @property in the header file, then you have to synthesize it using @synthesize in the implementation file.
Example:
.h class
@interface ExampleClass : NSObject
@property (nonatomic, retain) NSString *name;
@end
.m class@implementation ExampleClass
@synthesize name;
@end
Now the compiler will synthesize accessor methods for name. ExampleClass *newObject=[[ExampleClass alloc]init];
NSString *name1=[newObject name]; // get 'name'
[obj setName:@“Tiger”];
- List of attributes of @property :
- atomic.
- nonatomic.
- retain.
- copy.
- readonly.
- readwrite.
- assign.
- strong.
Example :
@property NSString *name; //by default atomic
@property (atomic)NSString *name; // explicitly declared atomic
@property (nonatomic)NSString *name;
- retain: is required when the attribute is a pointer to an object.The setter method will increase retain count of the object, so that it will occupy memory in autorelease pool.
- copy: If you use copy, you can't use retain. Using copy instance of the class will contain its own copy.
@property (copy) NSString *name;
NSMutableString *nameString = [NSMutableString stringWithString:@"Liza"];
xyzObj.name = nameString;
[nameString appendString:@"Pizza"];
readonly: If you don't want to allow the property to be changed via setter method, you can declare the property readonly.@property (readonly) NSString *name;
- readwrite: is the default behaviour. You don't need to specify readwrite attribute explicitly.
@property (readwrite) NSString *name;
- assign: will generate a setter which assigns the value to the instance variable directly, rather than copying or retaining it. This is best for primitive types like NSInteger and CGFloat, or objects you don't directly own, such as delegates.
@property (assign) NSInteger year;
Strong:-is a replacement for retain.
@property (nonatomic, strong) AVPlayer *player;
The
strong keyword implies ownership of the property. This is the default,
so you don't have to explicitly use the keyword if you intend for the
property to have a strong reference. If you declare a property as
strong, that property will stay in memory as long as at least one object
has a strong reference to it.
Weak:-
The weak keyword implies no
ownership or responsibility. If you declare a property as weak, it will
be released if the object has no strong references to it. Assigning an
object to a weak property does not increase the retain count of that
object as it would if the property were declared as strong.
- unsafe_unretained: There are a few classes in Cocoa and Cocoa Touch that don’t yet support weak references, which means you can’t declare a weak property or weak local variable to keep track of them. These classes include NSTextView, NSFont and NSColorSpace,etc. If you need to use a weak reference to one of these classes, you must use an unsafe reference.
An unsafe reference is similar to a weak reference in that it doesn’t keep its related object alive, but it won’t be set to nil if the destination object is deallocated.
@property (unsafe_unretained) NSObject *unsafeProperty;
Comments
Post a Comment