我在 applicationDidFinishLaunching 的应用程序委托中初始化了 RKClient 的 sharedClient,它运行良好。我在大多数应用程序中使用此客户端目标 URL,但是在 1 点,在 1 个类(播放器)中,我需要从 gravatar.com 加载用户的头像。所以,我让 Player 类定义了它自己的 RKClient 并符合 RKRequestDelegate 协议。然后它通过这个新的 RKClient 实例发出请求,并将请求的委托设置为 self。问题是我从来没有收到回复;那是
- (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,并仅将 sharedClient 用于 gravatar 请求。但是每当我尝试更改 RKClient sharedClient 的 baseURL 属性时,都可以通过以下任何一种方式:
[[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)