Reputation: 7826
I defined three properties, a UISearchBar, NSDicitonary type and NSArray type.
What's the differents between them? (self.) or (_)
And reason?
@property (nonatomic, strong, readwrite) UISearchBar *searchBar;
@property (nonatomic, strong) NSDictionary *citiesDataDic;
@property (nonatomic, strong) NSArray *initialOfCity;
The first way:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *key = [_initialOfCity objectAtIndex:section];
NSArray *citySection = [_citiesDataDic objectForKey:key];
return [citySection count];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"citydict"
ofType:@"plist"];
_citiesDataDic = [[NSDictionary alloc] initWithContentsOfFile:path];
_initialOfCity = [[_citiesDataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
_searchBar.placeholder = @"enter words";
_searchBar.delegate = self;
[_searchBar sizeToFit];
}
The second way:
NSString *key = [self.initialOfCity objectAtIndex:section];
NSArray *citySection = [self.citiesDataDic objectForKey:key];
return [citySection count];
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"citydict"
ofType:@"plist"];
self.citiesDataDic = [[NSDictionary alloc] initWithContentsOfFile:path];
self.initialOfCity = [[self.citiesDataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
self.searchBar.placeholder = @"enter words";
self.searchBar.delegate = self;
[self.searchBar sizeToFit];
Upvotes: 0
Views: 98
Reputation: 2439
hope this will help little more.
first read this https://stackoverflow.com/a/25759575/3767017
after going through it the next thing is self
if you do
self.name = @"Xyz";
the compiler change it into
[self setName:@"Xyz"]; //(calling setter method)
and same in the case of getting the variable , it will transalte it into [self name]
;
Upvotes: 1
Reputation: 3766
When you declare a property like -
@property (nonatomic, strong) NSDictionary *citiesDataDic;
That means you have 2 way to access it.
self.citiesDataDic or _citiesDataDic
_citiesDataDic is actually implicit ivar of property citiesDataDic. where self.citiesDataDic actually call setter/getter. like when you access self.citiesDataDic the getter method actually looks like
- (NSDictionary)citiesDataDic
{
return _citiesDataDic;
}
and setter method will be
- (void)setCitiesDataDic:(NSDictionary *)citiesDataDic
{
if(_citiesDataDic != citiesDataDic)
{
[_citiesDataDic release]; // release the old value it point
_citiesDataDic = [citiesDataDic retain]; // retain the new value
}
}
So basically when getting value both calling _citiesDataDic or self.citiesDataDic is same, setting the value first time is also same. But when reassigning value _citiesDataDic does not release old value (as you are directly accessing it),cause memory leak but self.citiesDataDic release old value, so no memory leak occurs.
These things are actually for Manual reference counting.
The term nonatomic or atomic is used for thread access control, if it's not atomic then several thread can access to setter/getter at the same time, while atomic control the simultaneous thread access through some kind of lock, in this case you need to add some kind locking setter/getter method.
Upvotes: -1
Reputation: 13424
readwrite
This qualifier is unnecessary as it is the default behavior. Don't use it or it might confuse people.
self.
syntax call the getter and setter of your property, which can be explicitly defined by propertyName
or setPropertyName
.
Try using self.
syntax as it makes things easier if one day you'll need a getter or setter.
Accessing property through _
syntax will access the property directly, even if getter/setter are defined.
Upvotes: 3