私のコードは正常に動作するようですが、Twitter アカウントがデバイスに保存されていない場合、アプリはこのエラーでクラッシュします Terminating app due to uncaught exception 'NSRangeException', reason: ' * -[__NSArrayI objectAtIndex:]: index 0 beyond bounds空の配列の場合'
- (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