kerno
kerno

Reputation: 187

NSArray using valueForKey and valueAtIndex

Longtime listener etc. I've looked through a lot of posts about NSDictionary and NSArray but I can't quite get this right, and I'm hoping for some clarification.

I have an NSArray masterArray with keys name, filename, fileObject

I'd like to display the name and filename in two separate labels in a UITableViewCell.

Currently I'm doing it like this (I've put everything in the cellForRowAtIndexPath method for this question's purposes)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];

NSArray *namesArray = [NSArray arrayWithArray:[masterArray valueForKey:@"name"]];
NSArray *filenamesArray = [NSArray arrayWithArray:[masterArray valueForKey:@"fileName"]];

NSString *name = [namesArray objectAtIndex:indexPath.row];
NSString *filename = [imageNameArray objectAtIndex:indexPath.row];

cell.nameLabel.text = name;
cell.filenameLabel.text = filename;

return cell;

}

My question is - is there no way to access NSArray (or NSDictionary) with a request like;

NSString *name = [masterArray valueAtIndex:indexPath.row valueForKey:@"name"];

Do you have to split it into smaller arrays/dicts and access these values separately, or can I do this more elegantly?

Upvotes: 1

Views: 579

Answers (1)

Evol Gate
Evol Gate

Reputation: 2257

You can use OrderedDictionary for iOS. What this third party APIs does is that it stores all the key/value pairs in an NSDictionary in an indexed format i.e in a continuous way just like NSArray. NSDictionary does not store values in an indexed/ ordered way. This 3rd party dictionary does.

Upvotes: 1

Related Questions