0
我做了兩個單獨的請求從外部來源獲取JSON,到目前爲止我已經實現了從第一個請求到我的表視圖的數據顯示。我的問題是,我需要將兩組數據組合到一個表視圖中,並通過一個公用密鑰對數據進行排序,在這種情況下,這是一個created_time。我知道我可以使用某種形式的數組,但我該如何去做這件事?多個數據源在單個表視圖
第一:
NSURL *url = [NSURL URLWithString:myURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
self.results = [json valueForKeyPath:@"data"];
[self.tableView reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}];
[operation start];
第二:
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/search/tweets.json"];
NSDictionary *parameters = @{@"count" : RESULTS_PERPAGE,
@"q" : encodedQuery};
SLRequest *slRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodGET
URL:url
parameters:parameters];
NSArray *accounts = [self.accountStore accountsWithAccountType:accountType];
slRequest.account = [accounts lastObject];
NSURLRequest *request = [slRequest preparedURLRequest];
dispatch_async(dispatch_get_main_queue(), ^{
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
});
請注意,您可以執行'[allResults sortUsingComparator:^ NSComparisonResult(NSDictionary * dict1,NSDictionary * dict2){'。另外,你應該明確實施Jason建議的檢查(事實上,它是一個NSDictionary)。否則,如果您的API發送錯誤的響應,則可能會導致應用程序崩潰。 –