Reputation: 6940
I have UITableView and NSDictionary. It populated like follow:
currentAlbumData = [album tr_tableRepresentation];
Where albums is simple NSObject class:
// h.file
@interface Album : NSObject
@property (nonatomic, copy, readonly) NSString *title, *artist, *genre, *coverUrl, *year;
-(id)initWithTitle:(NSString*)title artist:(NSString*)artist coverUrl:(NSString*)coverUrl year:(NSString*)year;
//m.file
-(id)initWithTitle:(NSString *)title artist:(NSString *)artist coverUrl:(NSString *)coverUrl year:(NSString *)year{
self = [super init];
if (self){
_title = title;
_artist = artist;
_coverUrl = coverUrl;
_year = year;
_genre = @"Pop";
}
return self;
};
And tr_tableRepresentation is category of Album class, returning NSDictionary:
//h.file
@interface Album (TableRepresentation)
- (NSDictionary*)tr_tableRepresentation;
@implementation Album (TableRepresentation)
//.m file
- (NSDictionary*)tr_tableRepresentation
{
return @{@"titles":@[@"Artist", @"Album", @"Genre", @"Year"],
@"values":@[self.artist, self.title, self.genre, self.year]};
}
That is the code i take from tutorial, so, in following lines we populate tableView data with NSDictionary values:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//... Cell initialization code
cell.textLabel.text = currentAlbumData[@"titles"][indexPath.row];
cell.detailTextLabel.text = currentAlbumData[@"values"][indexPath.row];
}
Now I'm stuck. because I'm getting confused when i see syntax like that.
cell.textLabel.text = currentAlbumData[@"titles"][indexPath.row];
cell.detailTextLabel.text = currentAlbumData[@"values"][indexPath.row];
What exactly is going on here? What this lines of code do? I can understand that we accessing @"titles"
and @"values"
somehow, could you please rewrite that lines in more readable manner, without square brackets?
And how could we even get @"titles"
and @"values"
using just indexPath (integer number)? That may sound kind of silly, but I'm not get it. I thought we have to put string as a parameter to access NSDictionary values, not an integer.
Upvotes: 0
Views: 158
Reputation: 993
Titles is an array so to get value at particular index you can use
cell.textlabel.text = [[currentAlbumData valueForKey:@"titles"] objectAtIndex:indexPath.row];
If you find this confusing then better store Titles inside an array and then use it below
NSArray *titles = [currentAlbumData valueForKey:@"titles"];
cell.textlabel.text = [titles objectAtIndex:indexPath.row];
Upvotes: 1
Reputation: 5655
titles
is the key for an NSArray of NSStrings. So is values
.
currentAlbumData[@"titles"]
asks the dictionary for the value at the titles
keypath. This returns an NSArray that is indexed by NSUIntegers, such as indexPath.row.
Upvotes: 1
Reputation: 4271
It is just a short way of writing code:
currentAlbumData[@"titles"][indexPath.row]
is same as [[currentAlbumData objectForKey:@"titles"] objectAtIndex:indexPath.row]
. Here, currentAlbumData
is a dictionary. You get it's object for key titles
, which is (supposedly) an array. Then you get the object at index indexPath.row
of this array.
Upvotes: 1