【发布时间】:2013-05-03 22:10:57
【问题描述】:
编辑- 已解决: 我为“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": {
我得到了bodyAsResponse 的NSLog 输出,但程序崩溃并且没有填写UITableView。我显然没有正确导航到 JSON?有什么想法吗?
感谢帮助,如果您需要更多代码 sn-ps-,请告诉我-
编辑- 崩溃是: 由于未捕获的异常“NSUnknownKeyException”而终止应用,原因:“[<__nscfstring> valueForUndefinedKey:]:此类不符合键标题的键值编码。”
【问题讨论】:
-
另外,最好显示映射配置而不是表格视图和空的 init 方法代码。
-
@Wain 感谢您的提示,我根据您的建议添加了崩溃和映射-
-
我认为您当前的 forKeyPath:@"feed.headline" 应该只是 forKeyPath:@"feed" 因为该 keypath 对您的 JSON 无效。
-
@Wain 我刚刚更改了它,现在得到这个:由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[
valueForUndefinedKey:]:这个类不符合键值编码用于关键链接。'
标签: ios xcode json uitableview restkit