我目前正在为twitter API做练习。目前,我有一个问题是将搜索标签结果填充到tableview cellForRowAtIndexPath方法。以下是我的代码。
我的标题代码:
@interface TableViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate> @property (nonatomic,strong) NSArray *array; @end
我的实施守则:
#import "TableViewController.h"
#import <Social/Social.h>
#import <Accounts/Accounts.h>
@interface TableViewController ()
@end
@implementation TableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self getDataFromTwitter];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSDictionary *dataDictionary = [self.array objectAtIndex:indexPath.row];
// NSLog(@"data result: %@", self.array);
cell.textLabel.text = dataDictionary[@"text"];
return cell;
}
- (void)getDataFromTwitter {
// 1. set an URL
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/search/tweets.json"];
// 2. set NSMutableArray For Parameter
NSMutableDictionary *parameter = [[NSMutableDictionary alloc] init];
[parameter setObject:@"%23MH370" forKey:@"q"];
[parameter setObject:@"10" forKey:@"text"];
[parameter setObject:@"popular" forKey:@"result_type"];
//3. set ACAccountStore & ACAccountType
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted == YES) {
NSArray *accountArray = [accountStore accountsWithAccountType:accountType];
if ([accountArray count] > 0) {
ACAccount *twitterAccount = [accountArray lastObject];
// guna slrequest to get data from twitter
SLRequest *getRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url parameters:parameter];
// set twitter account
[getRequest setAccount:twitterAccount];
[getRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:@"HTTP response status : %i", [urlResponse statusCode]];
NSLog(@"Output : %@", output);
self.array = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
if (self.array.count != 0) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
NSLog(@"search result: %@", self.array);
});
}
}];
}
}
}];
}
@end
我现在遇到一个问题,当我在运行应用程序时总是遇到崩溃,这是我的控制台的结果。
2014-03-12 19:57:11.990 twitterSearch[3069:a0b] -[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0xa45fd00
2014-03-12 19:57:11.992 twitterSearch[3069:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0xa45fd00'
答案 0 :(得分:0)
此错误表示您正在处理NSArray
作为NSDictionary
,很可能您在这里获取的字典不是数组:
self.array = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];