編輯 - 解決不拉JSON: 我添加了對了,我有映射問題的關懷「頻道」模型。謝謝!的UITableView在正確
我想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;
}
Headline.h
@interface Headline : NSObject
@property (strong, nonatomic) NSString *headline;
@property (strong, nonatomic) Links *linksHeadline;
+ (RKObjectMapping *) mapping;
@end
Headline.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?有任何想法嗎?
欣賞的幫助,讓我知道,如果你需要更多的代碼snippets-
編輯 - 崩潰是: 終止應用程序由於未捕獲的異常 'NSUnknownKeyException',原因:「[< __NSCFString 0xe693110> valueForUndefinedKey:]:該類不是關鍵標題的關鍵字編碼標準。「
什麼是崩潰? – Wain 2013-05-03 22:19:17
此外,更好地顯示您的映射配置比表視圖和空的init方法代碼。 – Wain 2013-05-03 22:20:27
@Wain感謝您的提示,我添加了崩潰,並根據您的建議添加了映射 - – Realinstomp 2013-05-03 22:25:30