私はiOSでブロックを使用するのが初めてで、おそらくそれが私の問題の核心だと考えています.
Restful サービスからデータをフェッチするだけの単純な静的 DataManager クラスを構築したいだけです。
さまざまな UIViewControllers (または collectionview/table コントローラー) からこれを呼び出します。
私のクラスでは、このような関数があります
+ (NSArray *) SearchByKeyword: (NSString*) keyword {
__block NSArray* searchResults = [[NSArray alloc] init];
NSString *baseURL = @"http://someURL.com/api/search";
NSString *requestURL = [baseURL stringByAppendingString:keyword];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseURL]];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
path:requestURL
parameters:nil];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
searchResults = [JSON valueForKeyPath:@""];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
return searchResults;
}
ただし、これはゼロデータを返し続けます。誰かがこれを行う正しい方法を提案できますか?