0

基本的に、AFNetworking フレームワークのコツをつかむために、スタック オーバーフローをしばらくブラウジングしました。AFHTTPClientを拡張したシングルトンクラスを作成することで、AFHTTPClientを使用することにしました。私が見たコードの一部は次のようになります。

 (InspectionClient*) sharedClient {

static InspectionClient *client = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    client = [[InspectionClient alloc] initWithBaseURL: [NSURL URLWithString:kServerName]];
});

return client;

}

- (id) initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (self) {

    // register operation class
    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
}
return self;
}

クライアントの新しいインスタンスを作成するときに、操作クラスを登録する必要があることに気付きました。JSON ファイルを送信するだけであれば、問題ないようです。しかし、私はクライアントをもっと普遍的にしたいと思っています。そうすれば、彼は写真を投稿したり、JSON をサーバーに投稿したりできます。このために、操作クラスの登録を解除して、新しいクラスを登録する必要がありますか?

4

1 に答える 1

2

のサブクラスを使用してAFHTTPClientいますが、オペレーション クラスをインスタンスに登録していません。の使用法について詳しくは、 registerHTTPOperationClass こちらを参照してください。

AFNetworkingまた、レジスタ操作クラスの使用法を理解するために、ソースのコメントを読むことをお勧めします。

/**
 Attempts to register a subclass of `AFHTTPRequestOperation`, 
 adding it to a chain to automatically generate request operations from a 
 URL request.

 @param operationClass The subclass of `AFHTTPRequestOperation` to register

 @return `YES` if the registration is successful, `NO` otherwise. 
 The only failure condition is if `operationClass` is not a subclass of
  `AFHTTPRequestOperation`.

 @discussion When `enqueueHTTPRequestOperationWithRequest:success:failure` 
 is invoked, each registered class is consulted in turn to see if it can 
 handle the specific request. The first class to return `YES` when sent a
 `canProcessRequest:` message is used to create an operation using 
 `initWithURLRequest:` and do `setCompletionBlockWithSuccess:failure:`. 
 There is no guarantee that all registered classes will be consulted. 
 Classes are consulted in the reverse order of their registration. 
 Attempting to register an already-registered class will move it to the 
 top of the list.
 */
于 2012-12-13T14:30:46.193 に答える