Ios -[\uu NSCFDictionary objectAtIndexedSubscript:]:发送到实例的选择器无法识别
我使用了一个教程将我的twitter主页包含在我的代码中,但对我来说不起作用 这是密码Ios -[\uu NSCFDictionary objectAtIndexedSubscript:]:发送到实例的选择器无法识别,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我使用了一个教程将我的twitter主页包含在我的代码中,但对我来说不起作用 这是密码 @implementation VSViewController - (void)viewDidLoad { [super viewDidLoad]; [self twitterTimeline]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Disp
@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.
它在这里崩溃,错误为-[\uu NSCFDictionary objectAtIndexedSubscript:]:无法识别的选择器发送到实例
NSDictionary *tweet = _array[indexPath.row];
cell.textLabel.text = tweet[@"text"];
return cell;
}
问题是
\u array
不是数组(它是字典)
您只需阅读错误消息即可看到这一点
您不能对JSONObjectWithData:options:error:
将为您提供什么类型的对象做出任何假设;这取决于JSON数据的外观。在本例中,JSON结构导致Objective-C等价物成为NSDictionary。您可能将数组
实例变量称为NSArray这一事实没有任何区别;在Objective-C中,对象就是它本身(多态性)
您之前没有发现这个问题,因为
count
是NSDictionary和NSArray的一种方法,所以当您说self.array.count
时没有崩溃,这就是为什么文本语法的访问器部分是voodoo。。。叫我暴躁鬼,但我从来没用过。。。我喜欢文本构造函数语法。