编辑-已解决: 我为“Feed”添加了一个模型,它解决了我遇到的映射问题。谢谢!
我想UITableView
在行中显示所有“标题”,但我遇到了崩溃:
WebListViewController.m -
@interface WebListViewController ()
@property (strong, nonatomic) NSArray *headlinesNow;
@end
@implementation WebListViewController
@synthesize tableView = _tableView;
@synthesize headlinesNow;
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *lAbbreviation = [defaults objectForKey:@"lAbbreviation1"];
NSString *tID = [defaults objectForKey:@"tId1"];
NSString *url = [NSString stringWithFormat:@"/now/?l=%@&t=%@&apikey=5xxxxxxxx", lAbbreviation, tID];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:url usingBlock:^(RKObjectLoader *loader) {
loader.onDidLoadObjects = ^(NSArray *objects) {
headlinesNow = objects;
[_tableView reloadData];
[loader.mappingProvider setMapping:[Headline mapping] forKeyPath:@"feed.headline"];
loader.onDidLoadResponse = ^(RKResponse *response){
NSLog(@"BodyAsString: %@", [response bodyAsString]);
};
}];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return headlinesNow.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Headline *headlineLocal = [headlinesNow objectAtIndex:indexPath.row];
NSString *headlineText = [NSString stringWithFormat:@"%@", headlineLocal.headline];
cell.textLabel.text = headlineText;
return cell;
}
标题.h
@interface Headline : NSObject
@property (strong, nonatomic) NSString *headline;
@property (strong, nonatomic) Links *linksHeadline;
+ (RKObjectMapping *) mapping;
@end
标题.m
@implementation Headline
+ (RKObjectMapping *)mapping {
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class] usingBlock:^(RKObjectMapping *mapping) {
[mapping mapKeyPathsToAttributes:
@"headline", @"headline",
nil];
[mapping hasOne:@"links" withMapping:[Links mapping]];
}];
return objectMapping;
}
@end
JSON 响应正文 -
{
"timestamp": "2013-05-03T22:03:45Z",
"resultsOffset": 0,
"status": "success",
"resultsLimit": 10,
"breakingNews": [],
"resultsCount": 341,
"feed": [{
"headline": "This is the first headline",
"lastModified": "2013-05-03T21:33:32Z",
"premium": false,
"links": {
"api": {
我正在获取 的NSLog
输出bodyAsResponse
,但程序崩溃并且没有填写UITableView
. 我显然没有正确导航到 JSON?有任何想法吗?
感谢您的帮助,如果您需要更多代码片段,请告诉我-
编辑-崩溃是:由于未捕获的异常“ NSUnknownKeyException ”而终止应用程序,原因:“[<__NSCFString 0xe693110> valueForUndefinedKey:]:此类不符合键标题的键值编码。”