【发布时间】:2013-06-11 02:24:57
【问题描述】:
我知道有一种更好/更简单的方法来创建一个由小型 NSDictionary 对象组成的 NSArray,可能使用 KVC 或一些 Obj-C 2.0 的奇思妙想,但我找不到它,也可以输入它. 我的课: @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;
...
方法:
- (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];
}
我的最终目标是用部分标题填充一个漂亮的 UITableView。我从 JSON API 调用中得到了所有这些东西,但它不是我想要的 TableView 的顺序。
【问题讨论】:
-
为什么要将字典放入数组中?
-
你所拥有的似乎是合理的。
-
UITableViewControllers 如果你的模型数据在一个 NSArray 中,工作起来非常方便。
-
@[]数组字面量? C 宏也总是一种选择。 -
这不仅是Cocoa touch,也是Cocoa。不确定其他 Objective-C 实现是否会支持它,但可能。
标签: objective-c cocoa-touch cocoa