Properties
Each time we've added an occurrence
variable to BNRItem, we've proclaimed and actualized a couple of accessor
strategies. Presently we will perceive how to utilize properties. Properties
are a helpful other option to working out accessors for example factors – one
that spares a considerable measure of writing and makes your class records much
clearer to peruse.
Pronouncing properties
A property is pronounced in the
interface of a class where strategies are proclaimed. A property statement has
the accompanying structure:
@property NSString *itemName;
When you announce a property, you are
verifiably proclaiming a setter and a getter for the occasion variable of a
similar name. So the above line of code is proportionate to the accompanying:
- (void)setItemName:(NSString *)str;
- (NSString *)itemName;
Every property has an arrangement of
properties that portray the conduct of the accessor strategies. The
characteristics are proclaimed in enclosures after the @property order. Here is
a case:
@property (nonatomic, readwrite, solid)
NSString *itemName;
There are three property qualities. Each
property has a few choices, one of which is the default and does not need to
expressly proclaimed.
The principal trait of a property has
two choices: nonatomic or nuclear. This credit needs to do with multi-strung
applications and is outside the extent of this book. Most Objective-C
developers regularly utilize nonatomic: we do at Big Nerd Ranch, thus does
Apple. In this book, we'll utilize nonatomic for all properties.
We should change BNRItem to utilize
properties rather than accessor techniques. In BNRItem.h, supplant the greater
part of your accessor techniques with properties that are nonatomic.
- (id)initWithItemName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber;
- (void)setItemName:(NSString *)str;
- (NSString *)itemName;
- (void)setSerialNumber:(NSString *)str;
- (NSString *)serialNumber;
- (void)setValueInDollars:(int)i;
- (int)valueInDollars;
- (NSDate *)dateCreated;
- (void)setContainedItem:(BNRItem *)i;
- (BNRItem *)containedItem;
- (void)setContainer:(BNRItem *)i;
- (BNRItem *)container;
@property (nonatomic) BNRItem
*containedItem;
@property (nonatomic) BNRItem
*container;
@property (nonatomic) NSString
*itemName;
@property (nonatomic) NSString
*serialNumber;
@property (nonatomic) int
valueInDollars;
@property (nonatomic) NSDate
*dateCreated;
@end
Sadly, nonatomic isn't the default
choice, so you will dependably need to unequivocally announce your properties
to be nonatomic.
The second quality of a property is
either readwrite or readonly. A readwrite property proclaims both a setter and
getter, and a readonly property just announces a getter. The default
alternative for this quality is readwrite. This is the thing that we need for
the majority of BNRItem's properties except for dateCreated, which ought to be
readonly. In BNRItem.h, proclaim dateCreated as a readonly property with the
goal that no setter strategy is announced for this occasion variable.
@property (nonatomic, readonly) NSDate
*dateCreated;
The last quality of a property depict
its memory administration. The most widely recognized choices let us know
whether the got to occurrence variable has a solid or powerless reference to
the question it focuses to. The default alternative is relegate, which is for
properties like valueInDollars that don't point to a question. Whatever is left
of BNRItem's occasion factors are for the most part solid references to objects
except for holder, which is powerless. Add the solid credit alternative to the
suitable properties and frail to the compartment property.
@property (nonatomic, solid) BNRItem
*containedItem;
@property (nonatomic, feeble) BNRItem
*container;
@property (nonatomic, solid) NSString
*itemName;
@property (nonatomic, solid) NSString
*serialNumber;
@property (nonatomic) int
valueInDollars;
@property (nonatomic, readonly, solid)
NSDate *dateCreated;
Construct and run the application. You
should see precisely the same as the last time you ran it. The main contrast is
that BNRItem.h is much more clean.
Incorporating properties
Notwithstanding utilizing a property to
proclaim accessor techniques, you can incorporate a property to produce the
code for the accessor strategies in the usage document. At the present time,
BNRItem.m characterizes the accessor techniques pronounced by every property.
For instance, the property itemName announces two accessor techniques, itemName
and setItemName:, and these are characterized in BNRItem.m like so:
- (void)setItemName:(NSString *)str
{
itemName = str;
}
- (NSString *)itemName
{
return itemName;
}
When you orchestrate a property, you
don't need to sort out the accessor definitions. You can blend a property by
utilizing the @synthesize mandate in the execution document. In BNRItem.m,
include a combine explanation for itemName and erase the executions of
setItemName: and itemName.
@implementation BNRItem
@synthesize itemName;
- (void)setItemName:(NSString *)str
{
itemName = str;
}
- (NSString *)itemName
{
return itemName;
}
You can integrate properties in the same
orchestrate proclamation or split them up into different articulations. In
BNRItem.m, blend whatever remains of the occasion factors and erase whatever is
left of the accessor usage.
@implementation
@synthesize itemName;
@synthesize containedItem, compartment,
serialNumber, valueInDollars,
dateCreated;
- (void)setSerialNumber:(NSString *)str
{
serialNumber = str;
}
- (NSString *)serialNumber
{
return serialNumber;
}
- (void)setValueInDollars:(int)i
{
valueInDollars = I;
}
- (int)valueInDollars
{
return valueInDollars;
}
- (NSDate *)dateCreated
{
return dateCreated;
}
- (void)setContainedItem:(BNRItem *)i
{
containedItem = I;
/When given a thing to contain, the
contained
/thing will be given a pointer to its
compartment
[i setContainer:self];
}
- (BNRItem *)containedItem
{
return containedItem;
}
- (void)setContainer:(BNRItem *)i
{
compartment = I;
}
- (BNRItem *)container
{
return compartment;
}
More details visit http://infocampus.co.in/ios-training-in-bangalore.html
No comments:
Post a Comment