我有 DiscussionBoard适用于 RestKit 的应用程序我正在移植 DBUser类到我的应用程序。唯一的区别是我的应用程序不会使用 CoreData。
我能够发布用户对象并将新的用户 ID 和 authentication_token 返回到我的应用程序。问题是响应没有被序列化为
一个用户对象。为什么 request:didLoadResponse:response
不会返回一个用户对象,但我可以在 signUpWithDelegate:delegate
你可以在这里看到我无法让 objectLoader:didLoadObjects
返回序列化对象? 为什么对象没有被序列化?
[12881:207]Loaded payload: {"user":{"id":"85","authentication_token":"_Udl98OO-xgmZOOJM26w","email":"ffff@adsfadfa.com"}}
[12881:207] _____ objectLoader didLoadObjects (
)
[12881:207] loginWasSuccessful (null)
AppDelegate.m
RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://spoonbook-2.local:3000/"];
[objectManager.router routeClass:[RKUser class] toResourcePath:@"/api/v1/authentication/create.json" forMethod:RKRequestMethodPOST];
// User
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[RKUser class]];
[userMapping mapKeyPath:@"id" toAttribute:@"userId"];
[userMapping mapAttributes:@"email", nil];
[userMapping mapAttributes:@"authentication_token", nil];
[objectManager.mappingProvider setObjectMapping:userMapping forKeyPath:@"user"];
RegisterViewController.m
-(IBAction)handleLoginClick:(id)sender
{
NSLog(@"handleLoginClick");
RKUser *user = [[[RKUser alloc] init] retain];
[user createWithEmail:_regEmailTF.text andPassword:_regPasswordTF.text delegate:self];
[user release];
}
用户.m
#import "RKUser.h"
#import <RestKit/RestKit.h>
static NSString* const kDBUserCurrentUserIDDefaultsKey = @"kDBUserCurrentUserIDDefaultsKey";
// Current User singleton
static RKUser* currentUser = nil;
@implementation RKUser
@synthesize userID = _userID;
@synthesize email = _email;
@synthesize password = _password;
@synthesize passwordConfirmation = _passwordConfirmation;
@synthesize delegate = _delegate;
@synthesize authentication_token = _authentication_token;
-(void)createWithEmail:(NSString *)username andPassword:(NSString *)password delegate:(NSObject<UserAuthenticationDelegate> *)delegate
{
_delegate = delegate;
RKObjectManager*objectManager = [RKObjectManager sharedManager];
RKObjectLoader* objectLoader = [objectManager objectLoaderWithResourcePath:@"/api/v1/authentication/create.json" delegate:self];
objectLoader.method = RKRequestMethodPOST;
objectLoader.params = [NSDictionary dictionaryWithKeysAndObjects:@"user[email]", username, @"user[password]", password, @"user[password_confirmation]", password, nil];
objectLoader.targetObject = self;
// try adding a userMapping?
RKObjectMapping *userMapping = [objectManager.mappingProvider objectMappingForKeyPath:@"user"];
objectLoader.objectMapping = userMapping;
// TODO: Temporary to work around bug in Rails serialization on the Heroku app
objectLoader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForKeyPath:@"user"];
[objectLoader send];
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response
{
NSLog(@"ReviewFormViewController Loaded payload: %@", [response bodyAsString]);
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray *)objects
{
// NOTE: We don't need objects because self is the target of the mapping operation
NSLog(@"_____ objectLoader didLoadObjects %@", objects);
if ([objectLoader wasSentToResourcePath:@"/api/v1/authentication/login.json"]) {
// Login was successful
[self loginWasSuccessful];
} else if ([objectLoader wasSentToResourcePath:@"/api/v1/authentication/create.json"]) {
// Sign Up was successful
if ([self.delegate respondsToSelector:@selector(userDidSignUp:)]) {
[self.delegate userDidSignUp:self];
}
// Complete the login as well
[self loginWasSuccessful];
}
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError*)error {
...snip...
}
- (void)loginWasSuccessful {
...snip...
}
@end
最佳答案
我认为您需要使用它才能映射响应。
/**
Send the data for a mappable object by performing an HTTP POST. The data returned in the response will be mapped according
to the object mapping provided.
*/
- (RKObjectLoader*)postObject:(id<NSObject>)object mapResponseWith:(RKObjectMapping*)objectMapping delegate:(id<RKObjectLoaderDelegate>)delegate;
关于objective-c - RestKit - 在示例应用程序中映射用户对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6644647/