iOS: Creating arrays and dictionaries a bit easier
In other languages, you can often do the following to get
or set an item from an array:
You can simplify this to:
array[index] = . . .;
But Objective-C has always required you to use verbose methods such as:
But Objective-C has always required you to use verbose methods such as:
[array objectAtIndex:. . .]
Happy days have arrived, for the creators of Objective-C have finally brought us the goodies. From now on, you can simply use [ ] brackets to index arrays, dictionaries, and even your own classes!
MasterViewController.m calls objectAtIndex: in quite a few places, for example in tableView:titleForHeaderInSection:. That method currently looks like this:
You can now simplify it to the following:
That looks a lot more natural, especially if you have programmed in other languages before. (It’s also the syntax C uses for regular C arrays.)
Another example is tableView:numberOfRowsInSection:
Happy days have arrived, for the creators of Objective-C have finally brought us the goodies. From now on, you can simply use [ ] brackets to index arrays, dictionaries, and even your own classes!
MasterViewController.m calls objectAtIndex: in quite a few places, for example in tableView:titleForHeaderInSection:. That method currently looks like this:
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
if (sortedByName)
return [sortedSectionNames objectAtIndex:section]; else
{
if (sortedByName)
return [sortedSectionNames objectAtIndex:section]; else
return nil;
}
You can now simplify it to the following:
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
if (sortedByName)
return sortedSectionNames[section]; else
{
if (sortedByName)
return sortedSectionNames[section]; else
return nil;
}
That looks a lot more natural, especially if you have programmed in other languages before. (It’s also the syntax C uses for regular C arrays.)
Another example is tableView:numberOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
if (sortedByName)
{
if (sortedByName)
{
NSString *sectionName = [sortedSectionNames
objectAtIndex:section]; return [[namesDictionary objectForKey:sectionName]
NSString *sectionName = [sortedSectionNames
objectAtIndex:section]; return [[namesDictionary objectForKey:sectionName]
count];
}
else
else
{
return [sortedValues count];
}
}
return [sortedValues count];
}
}
You can simplify this to:
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
if (sortedByName) {
NSString *sectionName = sortedSectionNames[section];
return [namesDictionary[sectionName] count]; }
else
{
return [sortedValues count];
} }
{
if (sortedByName) {
NSString *sectionName = sortedSectionNames[section];
return [namesDictionary[sectionName] count]; }
else
{
return [sortedValues count];
} }
iOS: Creating arrays and dictionaries a bit easier
Reviewed by Unknown
on
16:09
Rating:
No hay comentarios: