0

看来我的代码运行良好,但如果设备中没有存储 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

4

3 回答 3

2

将您更改if为仅在您的数组中有对象时执行,您可以检查count

if (twitterAccounts.count)
于 2013-10-10T04:10:54.530 回答
0

你打电话ACAccount *currentAccount = [twitterAccounts objectAtIndex:0];没有先检查是否有任何帐户。

一种选择是更改:

if (twitterAccounts != nil)

至:

if (twitterAccounts.count) {
    // process the account
} else {
    // show alert
}
于 2013-10-10T04:10:21.950 回答
0

考虑将原始代码从

 if (twitterAccounts != nil)

if (twitterAccounts != nil && twitterAccounts.count)
于 2013-10-10T04:31:58.720 回答