【问题标题】:Restkit without core data example没有核心数据示例的 Restkit
【发布时间】:2014-01-11 21:11:20
【问题描述】:

我设法用 cocoapods 安装了 RestKit,但现在我对它包含的所有不同类感到困惑。

假设我得到了课程:

@interface Actor : NSObject

@property (strong, nonatomic) NSString *firstName;
@property (strong, nonatomic) NSString *lastName;
@property (strong, nonatomic) NSNumber *age;

 @end

并且网址“http://SomeWebSite.com/actors”返回 json:

{
   Actors:
   [
      {
         "firstName": "Will",
         "lastName": "Ferrell",
         "age": 25
      },
      {
         "firstName": "Jim", 
         "lastName": "carrey",
         "age": 25
      } 
   ]
}

有人可以举一个完整的例子来说明如何从 url 中获取一组人员的数据吗?

非常感谢!

【问题讨论】:

    标签: ios rest restkit restkit-0.20


    【解决方案1】:

    类似这样的:

        RKObjectMapping* actorMapping = [RKObjectMapping mappingForClass:[Actor class]];
        [actorMapping addAttributeMappingsFromDictionary:@{ 
            @"firstName": @"firstName",
            @"lastName": @"lastName",
            @"age": @"age",
          }];
    
        RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:actorMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"Actors" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    
        NSURL *URL = [NSURL URLWithString:@"http://SomeWebSite.com/actors"];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];
        RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
        [objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
            RKLogInfo(@"Load collection of Actors: %@", mappingResult.array);
        } failure:^(RKObjectRequestOperation *operation, NSError *error) {
            RKLogError(@"Operation failed with error: %@", error);
        }];
    
        [objectRequestOperation start];
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多