gpt4 book ai didi

ios - -[__NSCFDictionary objectAtIndexedSubscript :]: unrecognized selector sent to instance

转载 作者:可可西里 更新时间:2023-11-01 06:21:45 34 4
gpt4 key购买 nike

我使用一个教程将我的 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.

它在这里崩溃并出现错误 -[__NSCFDictionary objectAtIndexedSubscript:]: 无法识别的选择器发送到实例

NSDictionary *tweet = _array[indexPath.row];

cell.textLabel.text = tweet[@"text"];

return cell;
}

最佳答案

问题是 _array 不是数组(它是字典)。

您只需阅读错误消息即可看到这一点。

您不能对 JSONObjectWithData:options:error: 会给您什么样的对象做出任何假设;这取决于 JSON 数据的外观。在这种情况下,JSON 结构导致 Objective-C 等效项成为 NSDictionary。事实上,您可能已经调用您的 array 实例变量 NSArray 没有区别;在 Objective-C 中,对象就是它的本来面目(多态性)。

你没有早点发现问题,因为 count 是 NSDictionary 和 NSArray 的方法,所以当你说 self.array.count 时你没有崩溃>.

关于ios - -[__NSCFDictionary objectAtIndexedSubscript :]: unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23846281/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com