0

Here is my 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":""]
              }]
   }

one class Attachments and one class articles

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

in 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];
    }

i get this error

[RKObjectMappingOperation transformValue:atKeyPath:toType:] Failed transformation of value at keyPath 'attachments'. No strategy for transforming from '__NSArrayM' to 'Attachments'

What's wrong?

Thanks

4

1 回答 1

0

Change

@property (strong, nonatomic) Attachments *attachments;

To

@property (strong, nonatomic) NSMutableArray *attachments;

The relationship mapping returns an array of data, even if there's only one.

于 2012-12-12T22:04:17.630 回答