这是我的json
{
"status":"ok",
"count":1,
"count_total":164,
"pages":164,
"posts":[{"id":2966,
"type":"post",
"title":"title here",
"content":"here content",
"date":"",
"attachments [
"comment_count":0,
"thumbnail":""]
}]
}
一个附件和一个类文章
Attachments.h
#import <Foundation/Foundation.h>
@interface Attachments : NSObject
@property (strong, nonatomic) NSString *thumbnail;
@end
Attachments.m
#import "Attachments.h"
@implementation Attachments
@synthesize thumbnail;
@end
articles.h
#import <UIKit/UIKit.h>
#import "Attachments.h"
@interface articles : NSObject
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) Attachments *attachments;
@property (strong, nonatomic) NSString *date;
@end
articles.m
#import "articles.h"
@implementation articles
@synthesize title;
@synthesize attachments;
@synthesize date;
@end
viewcontroller.h
#import <UIKit/UIKit.h>
#import <RestKit/RestKit.h>
#import "articles.h"
#import "postCell.h"
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, RKObjectLoaderDelegate> {
__weak IBOutlet UISearchBar *_searchBar;
__weak IBOutlet UITableView *_tableView;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) NSMutableArray *post;
@end
在viewcontroller.m中
RKObjectMapping* attachmentsMapping = [RKObjectMapping mappingForClass:[Attachments class]];
[attachmentsMapping mapKeyPathsToAttributes:@"thumbnail", @"thumbnail", nil];
RKObjectMapping *postMapping = [RKObjectMapping mappingForClass:[articles class]];
[postMapping mapKeyPathsToAttributes:@"title",@"title",@"date",@"date",nil];
[postMapping mapKeyPath:@"attachments" toRelationship:@"attachments" withMapping:attachmentsMapping];
[objectManager.mappingProvider setMapping:postMapping forKeyPath:@"posts"];
[self searchRequest];
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects {
NSLog(@"objects[%d]", [objects count]);
[posts removeAllObjects];
for (articles *t in objects) {
[posts addObject:t];
}
[self.tableView reloadData];
}
我收到此错误
[RKObjectMappingOperation transformValue:atKeyPath:toType:]失败 keyPath'附件'的价值转换。没有策略 从'__NSArrayM'转换为'附件'
怎么了?
由于
答案 0 :(得分:0)
更改
@property (strong, nonatomic) Attachments *attachments;
要
@property (strong, nonatomic) NSMutableArray *attachments;
关系映射返回一个数据数组,即使只有一个数据。