race_carr
race_carr

Reputation: 1377

Please help me avoid future violation of DRY principle

I know there is a much better/easier way to create an NSArray of small little NSDictionary objects, probably using KVC or some Obj-C 2.0 fanciness, but I couldn't find it and was just as well to type it up. My class: @interface PDDetailShowModel : NSObject

@property(nonatomic, strong, readonly)NSString *showid;
@property(nonatomic, strong, readonly)NSString *showdate;
@property(nonatomic, strong, readonly)NSString *showyear;
@property(nonatomic, strong, readonly)NSString *city;
@property(nonatomic, strong, readonly)NSString *state;
@property(nonatomic, strong, readonly)NSString *country;
...

The method:

- (NSArray *) createArrayForTableView
{
  NSMutableArray *list = [NSMutableArray array];
  NSDictionary *dict = @{@"venue":_venue};
  [list addObject:dict];
  NSDictionary *dict1 = @{@"city":_city};
  [list addObject:dict1];
  NSDictionary *dict2 = @{@"state":_state};
  [list addObject:dict2];
  NSDictionary *dict3 = @{@"country":_country};
  ....
  return [NSArray arrayWithArray:list];
}

My end goal is to populate a nice UITableView with section headers. I got all this stuff from a JSON API call, but it's not in the order that I want for the TableView.

Upvotes: 0

Views: 100

Answers (1)

uchuugaka
uchuugaka

Reputation: 12782

You could wrap the dictionary literals in an array literal.

Alternatively, to avoid recompiling with changes, or to avoid unnecessary memory consumption, you can create a plist in your project and edit that when needed and simply initialize the array with initWithContentsOfURL: [[NSBundle mainBundle] URLForResource: @"plistFileName"];

Upvotes: 2

Related Questions