0

It appears my code runs fine, but if no Twitter Accounts are stored in the device, the app will crash with this error Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'

- (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];
}

I'm not sure why, as I do have the nil check as seen above once it's created? ACAccount *currentAccount = [twitterAccounts objectAtIndex:0]; How would I display an UIAlertView or something similar if no accounts are found on device, rather then having it crash?

2
  • 3
    Simply check if([twitterAccounts count] > 0) then do your work otherwise array is empty. Commented Oct 10, 2013 at 4:13
  • To checking if(twitterAccounts!=nil) doesn't check if there is index available to fetch the object. It just check twitterAccounts have been initialized or not. While the error is saying that there is no index 0 available in twitterAccounts. So for that if ([twitterAccounts count] > index){NSLog(@"%@",twitterAccounts[index])}. Thats it!! Commented Oct 10, 2013 at 8:08

3 Answers 3

2

Change your if to only execute when your array has objects in it, that you can check with count

if (twitterAccounts.count)
Sign up to request clarification or add additional context in comments.

1 Comment

This if condition will be FALSE if array is empty.
0

You call ACAccount *currentAccount = [twitterAccounts objectAtIndex:0]; without first checking to see if there are any accounts.

One option would be to change:

if (twitterAccounts != nil)

to:

if (twitterAccounts.count) {
    // process the account
} else {
    // show alert
}

Comments

0

Consider changing the original code from

 if (twitterAccounts != nil)

to

if (twitterAccounts != nil && twitterAccounts.count)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.