2012-04-07 50 views
0

我在applicationDidFinishLaunching的应用程序委托中初始化RKClient的sharedClient,它工作的很好。我在大多数应用程序中使用这个客户端的目标URL,但是在1个类(播放器)中,我需要从gravatar.com加载用户的头像。所以,我有Player类定义它自己的RKClient并符合RKRequestDelegate协议。然后它通过这个新的RKClient实例发出请求,并将请求的委托设置为self。问题是我从来没有收到回应;即RestKit:多个RKClients /更改baseURL

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response 

永远不会被调用。这里是整个代码示例:

// Player.m 

#import "Player.h" 

@implementation Player 

# pragma mark - Accessor Synthesizers 

@synthesize identifier = _identifier; 
@synthesize name = _name; 
@synthesize story = _story; 
@synthesize emailHash = _emailHash; 
@synthesize pointPercentage = _pointPercentage; 
@synthesize hitPercentage = _hitPercentage; 
@synthesize lastCups = _lastCups; 
@synthesize shotCount = _shotCount; 
@synthesize hitCount = _hitCount; 
@synthesize wins = _wins; 
@synthesize losses = _losses; 
@synthesize gravatar = _gravatar; 

# pragma mark - Instance Methods 

- (void)getGravatar { 
    RKClient *gClient = [RKClient clientWithBaseURL:[NSURL URLWithString:@"http://gravatar.com"]]; 
    NSString *path = [self gravatarLink]; 
    NSLog(@"Getting Gravatar With Link: http://gravatar.com%@", path); 
    [gClient get:path delegate:self]; 
} 

- (NSString *)description { 
    return [NSString stringWithFormat:@"{Season: [id=%i, name=%@, story=%@ ]}", _identifier, _name, _story]; 
} 

# pragma mark - RKRequest Delegate Methods 

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response { 
    NSLog(@"Gravatar Back: %@", response.bodyAsString); 
    self.gravatar = response.body; 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"GravatarLoaded" object:self]; 
} 

# pragma mark - Private Methods 

- (NSString *)gravatarLink { 
    NSString *path; 
    path = [NSString stringWithFormat:@"/avatar/%@?d=monsterid&r=x", _emailHash]; 
    if([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale]==2) 
     return [path stringByAppendingString:@"&s=200"]; 
    else 
     return [path stringByAppendingString:@"&s=100"]; 
} 

@end 

另外,我还试图改变sharedClient的基本URL,只是使用了的gravatar请求sharedClient。但每当我试图改变RKClient sharedClient的基本URL特性,通过以下两种方式:

[[RKClient sharedClient] setBaseURL:[NSURL URLWithString:@"http://gravatar.com"]]; 

[RKClient sharedClient].baseURL: [NSURL URLWithString:@"http://gravatar.com"]; 

我得到一个运行时错误:

2012-04-07 15:37:23.565 MLP[34403:fb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL URLByAppendingResourcePath:queryParameters:]: unrecognized selector sent to instance 0x8bcdd50' 
*** First throw call stack: 
(0x1b7c022 0x1f58cd6 0x1b7dcbd 0x1ae2ed0 0x1ae2cb2 0x23eb5 0x24032 0x6ddb 0xdda2 0xc465c5 0xc467fa 0x14db85d 0x1b50936 0x1b503d7 0x1ab3790 0x1ab2d84 0x1ab2c9b 0x20407d8 0x204088a 0xbb5626 0x27cd 0x2735) 
terminate called throwing an exception(lldb) 

回答

0

clientWithBaseURL:方法预计NSString,而不是NSURL。尝试使用

[[RKClient sharedClient] setBaseURL:@"http://gravatar.com"]; 
0

你只需要到Targets->构建设置 - >其他链接器标记

正如RestKit教程中指定我们改变添加了一个名为“-ObjC-all_load”标志现在编辑那只显示“-ObjC”

这必须为你起锅。

1

我用:

[[RKClient sharedClient] setBaseURL:[RKURL URLWithString:@"http://gravatar.com"]]; 

为我工作。