我使用了一个教程在我的代码中包含My twitter主页,但对我来说不起作用。
这是代码
@implementation VSViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self twitterTimeline];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)twitterTimeline {
ACAccountStore *account = [[ACAccountStore alloc] init]; // Creates AccountStore object.
// Asks for the Twitter accounts configured on the device.
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
{
// If we have access to the Twitter accounts configured on the device we will contact the Twitter API.
if (granted == YES){
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType]; // Retrieves an array of Twitter accounts configured on the device.
// If there is a leat one account we will contact the Twitter API.
if ([arrayOfAccounts count] > 0) {
ACAccount *twitterAccount = [arrayOfAccounts lastObject]; // Sets the last account on the device to the twitterAccount variable.
NSURL *requestAPI = [NSURL URLWithString:@"http://api.twitter.com/1.1/statuses/user_timeline.json"]; // API call that returns entires in a user's timeline.
// The requestAPI requires us to tell it how much data to return so we use a NSDictionary to set the 'count'.
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:@"100" forKey:@"count"];
[parameters setObject:@"1" forKey:@"include_entities"];
// This is where we are getting the data using SLRequest.
SLRequest *posts = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:requestAPI parameters:parameters];
posts.account = twitterAccount;
// The postRequest: method call now accesses the NSData object returned.
[posts performRequestWithHandler:
^(NSData *response, NSHTTPURLResponse
*urlResponse, NSError *error)
{
// The NSJSONSerialization class is then used to parse the data returned and assign it to our array.
self.array = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
if (self.array.count != 0) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData]; // Here we tell the table view to reload the data it just recieved.
});
}
}];
}
} else {
// Handle failure to get account access
NSLog(@"%@", [error localizedDescription]);
}
}];
}
#pragma mark Table View Data Source Mehtods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Returns the number of rows for the table view using the array instance variable.
return [_array count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Creates each cell for the table view.
static NSString *cellID = @"CELLID" ;
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
// Creates an NSDictionary that holds the user's posts and then loads the data into each cell of the table view.
IT崩溃错误 - [__ NSCFDictionary objectAtIndexedSubscript:]:无法识别的选择器发送到实例
NSDictionary *tweet = _array[indexPath.row];
cell.textLabel.text = tweet[@"text"];
return cell;
}
答案 0 :(得分:9)
问题是_array
不是数组(它是字典)。
您只需阅读错误消息即可查看。
你不能对JSONObjectWithData:options:error:
会给你什么样的对象做出任何假设;这取决于JSON数据的样子。在这种情况下,JSON结构导致Objective-C等效为NSDictionary。您可能调用您的array
实例变量和NSArray的事实没有区别;在Objective-C中,一个对象就是它(多态)。
您之前没有发现问题,因为count
是NSDictionary和NSArray的方法,因此当您说self.array.count
时没有崩溃。