0

applicationDidFinishLaunchingのアプリケーションデリゲートでRKClientのsharedClientを初期化すると、うまく機能します。ほとんどのアプリケーションでこのクライアントの宛先URLを使用していますが、ある時点で、1つのクラス(プレーヤー)で、gravatar.comからユーザーのアバターを読み込む必要があります。そこで、Playerクラスに独自のRKClientを定義させ、RKRequestDelegateプロトコルに準拠させました。次に、この新しいRKClientインスタンス化を介してリクエストを作成し、リクエストのデリゲートを自分自身に設定します。問題は、私が応答を受け取らないことです。あれは

- (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の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

このclientWithBaseURL:メソッドはNSStringではなく を想定していNSURLます。使ってみて

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

Targets-> build Settings —> other Linker Flags に移動するだけです。

RestKit チュートリアルで指定されているように、「-ObjC-all_load」というフラグを追加し、「-ObjC」のみを表示するように編集しました</p>

これはあなたのために中華鍋でなければなりません。

于 2012-08-21T08:57:19.587 に答える