gpt4 book ai didi

ios - index 0 beyond bounds for empty array - 找到数组但如何修复?

转载 作者:行者123 更新时间:2023-11-28 22:24:44 26 4
gpt4 key购买 nike

看来我的代码运行良好,但如果设备中没有存储 Twitter 帐户,应用程序将崩溃并出现此错误 Terminating app due to uncaught exception 'NSRangeException', reason: '* - [__NSArrayI objectAtIndex:]: 索引 0 超出空数组的界限'

- (void)viewDidLoad
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
if (accountStore != nil)
{
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
if (accountType != nil)
{
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted)
{
NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
if (twitterAccounts != nil)
{

ACAccount *currentAccount = [twitterAccounts objectAtIndex:0];
if (currentAccount != nil)
{
NSString *friendListString = @"https://api.twitter.com/1.1/friends/list.json?cursor=-1&skip_status=true&include_user_entities=false";


SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:friendListString] parameters:nil];
if (request != nil)
{
// 1.1 api requires a user to be logged in.
[request setAccount:currentAccount];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSInteger responseCode = [urlResponse statusCode];
if (responseCode == 200)
{
// ADD STUFFS HERE

}

[theCollectionView reloadData];

}
}
}];
}
}
}
}
else
{
NSLog(@"User did not grant access.");
}

}];
}
}
[super viewDidLoad];
}

我不确定为什么,因为创建后我确实有上面看到的 nil 检查? ACAccount *currentAccount = [twitterAccounts objectAtIndex:0];我将如何显示 UIAlertView或者如果在设备上没有找到帐户,而不是让它崩溃,则类似的东西?

最佳答案

if 更改为仅在数组中包含对象时执行,您可以使用 count 进行检查

if (twitterAccounts.count)

关于ios - index 0 beyond bounds for empty array - 找到数组但如何修复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19286898/

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