0

我在 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)
4

3 回答 3

1

我用了:

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

这对我有用。

于 2012-10-02T21:28:33.313 回答
0

你只需要去 Targets-> build Settings -> other Linker Flags

正如在 RestKit 教程中指定的那样,我们添加了一个名为“-ObjC-all_load”的标志,现在将其编辑为只显示“-ObjC”</p>

这一定适合你。

于 2012-08-21T08:57:19.587 回答
0

clientWithBaseURL:方法需要一个NSString,而不是一个NSURL。尝试使用

[[RKClient sharedClient] setBaseURL:@"http://gravatar.com"];
于 2012-04-10T13:53:06.737 回答